Location | Community
Skip to main content
March 10, 2016
Solved

Location

  • March 10, 2016
  • 2 replies
  • 1100 views

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.
Best answer by Kunal_Gaba_

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

kautuk_sahni
Employee
March 14, 2016

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
Kunal_Gaba_
Kunal_Gaba_Accepted solution
New Participant
March 10, 2016

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