Expand my Community achievements bar.

SOLVED

Location

Avatar

Former Community Member

Is It possible to get the exact user location i.e; city, and state without using the clientcontext method and google api's

1 Accepted Solution

Avatar

Correct answer by
Employee Advisor

You can use W3C Geolocation APIs to get the latitude and longitude and then call the Google Maps API to get the city, state information. 

http://www.w3schools.com/html/html5_geolocation.asp

http://stackoverflow.com/questions/6797569/get-city-name-using-geolocation

View solution in original post

2 Replies

Avatar

Correct answer by
Employee Advisor

You can use W3C Geolocation APIs to get the latitude and longitude and then call the Google Maps API to get the city, state information. 

http://www.w3schools.com/html/html5_geolocation.asp

http://stackoverflow.com/questions/6797569/get-city-name-using-geolocation

Avatar

Administrator

Hi

Adding to Kunal's comment,

You can read more about it at http://viralpatel.net/blogs/html5-geolocation-api-tutorial-example/

//

IP Lookup in JavaScript

This method is good because it's easy to implement and is fairly accurate at a country level but accuracy drops considerably for anything more specific.

// API key excluded for brevity, see full demo.var apiurl = 'http://api.ipinfodb.com/v3/ip-city';$.getJSON(apiurl+'/format=json&callback=?',function(data){$("h3#location").html(data.regionName + ", " + data.countryName);});

Geolocation API

Although by no means perfect, this method is as accurate as it gets. User is prompted to share geolocation details which may reduce usage.

navigator.geolocation.getCurrentPosition(function(pos){$.getJSON(apiurl+'/format=json&callback=?',function(data){var regionName = data[...]AdministrativeAreaName;var countryName = data[...]CountryName;$("h3#location").html(regionName + ", " + countryName);});});

In there you'll see I used the Google Maps API v3 by way of YQL so that a JSON callback is possible.

Full demo: http://jsfiddle.net/ZjXXh

 

 

I hope this will help you.

Thanks and Regards

KautuK Sahni



Kautuk Sahni