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

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)

Flyleaf, I work at SmartyStreets where we also provide an API to get the coordinates of addresses; it's called LiveAddress API.

As Bart mentioned, Google's TOS won't allow you to geocode without showing a Google Map, and what's more is you can't store the results. If you're looking to locate an address as you've suggested, I recommend something like LiveAddress which will actually verify the validity of the address as well -- these other APIs and HTML5 will not do that.

Hope you find something that works for you. The question right now is a little vague, but maybe this will help. It might go something like this (with Javascript):

LiveAddress.geocode(address, function(geo) {
    alert("The address is at: " + geo.coords);
});