Your achievements

Level 1

0% to

Level 2

Tip /
Sign in

Sign in to Community

to gain points, level up, and earn exciting badges like the new
Bedrock Mission!

Learn more

View all

Sign in to view all badges

SOLVED

Location

Anonymous
Not applicable

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
kunal23
Correct answer by
Level 10
Level 10

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
kunal23
Correct answer by
Level 10
Level 10

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

kautuk_sahni
Employee
Employee

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