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

后端 未结 5 1212
清歌不尽
清歌不尽 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: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)

提交回复
热议问题