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

后端 未结 5 1206
清歌不尽
清歌不尽 2021-02-04 18:52

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 cou

相关标签:
5条回答
  • 2021-02-04 19:06

    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.

    0 讨论(0)
  • 2021-02-04 19:12

    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 the getCurrentPosition method is for.

    0 讨论(0)
  • 2021-02-04 19:14

    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.

    0 讨论(0)
  • 2021-02-04 19:16

    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);
    });
    
    0 讨论(0)
  • 2021-02-04 19:26
    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)
    0 讨论(0)
提交回复
热议问题