简体   繁体   中英

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

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

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);
});

I found the best option to be to use OpenStreetMap directly via a sub-project ( Nominatim ) API they have.

API documentation

Example REST call: http://nominatim.openstreetmap.org/reverse?format=xml&lat=35.958&lon=-83.952&zoom=18

Note that while their terms of service does allow direct client-side use from browser JavaScript, they don't want high-volume use and suggest using your server as a proxy to add caching and so it can be easily switched. The data is available for download, and instructions are provided for setting up a locally query-able database of the data.

Check out Gisgraphy a free an open source geo service.

Everyone will tell you to use the Google Maps API but read the terms of use .

(g) No Use of Content without a Google Map. You must not use or display the Content without a corresponding Google map, unless you are explicitly permitted to do so in the Maps APIs Documentation, or through written permission from Google. In any event, you must not use or display the Content on or in conjunction with a non-Google map. For example, you must not use geocodes obtained through the Service in conjunction with a non-Google map. As another example, you must not display Street View imagery alongside a non-Google map, but you may display Street View imagery without a corresponding Google map because the Maps APIs Documentation explicitly permits you to do so.

So with the Google API you must use a Google Map . So if you need a map you're OK.

It will help if I can get that latitude and longitude using an HTML5 geolocation

Well, then just do exactly that – where exactly is your problem with it?

That's what thegetCurrentPosition method is for .

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM