flyleaf flyleaf - 3 months ago 13
Javascript Question

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?

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


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