Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How do I get an addresses latitude-longitude using HTML5 geolocation or Google API?

I am getting baffled with a problem that is, I can get the location of a user from Facebook and all I need is to get the latitude and longitude of that location, but I still couldn't find a solution.

It will help if I can get that latitude and longitude using an HTML5 geolocation or any Google API. What is the solution?

like image 373
flyleaf Avatar asked Jul 26 '12 11:07

flyleaf


1 Answers

if (navigator.geolocation) {
    navigator.geolocation.getCurrentPosition(successFunction, errorFunction);
} else {
    alert('It seems like Geolocation, which is required for this page, is not enabled in your browser. Please use a browser which supports it.');
}

If the browser supports geolocation and if getCurrentPosition runs successfully, then a success function is called. And then in the function successFunction

have


function successFunction(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    console.log('Your latitude is :'+lat+' and longitude is '+long);
}

Read more on the Geolocation API here

  • http://dev.opera.com/articles/view/how-to-use-the-w3c-geolocation-api/ (Disclaimer: my article)
  • http://dev.w3.org/geo/api/spec-source.html (The Spec)
like image 149
shwetank Avatar answered Sep 20 '22 23:09

shwetank