Skip to main content
Mar 10, 2016 8:39 PM UTC
Solved

Location

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

This post is no longer active and is closed to new replies. Need help? Start a new post to ask your question.
Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Member
Mar 10, 2016 8:57 PM UTC

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

2 replies

Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Member
Mar 10, 2016 8:57 PM UTC

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

Translate
kautuk_sahni
Level 9
Mar 14, 2016 7:36 AM UTC

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

Translate
Kautuk Sahni