如何使用HTML5地理位置或Google API获取地址纬度经度?

fly*_*eaf 3 javascript html5 geolocation latitude-longitude

我感到困惑的一个问题是,我可以从Facebook获得用户的位置,我只需要获得该位置的经纬度,但我仍然找不到解决方案.

如果我可以使用HTML5地理位置或任何Google API获得经度和纬度,这将有所帮助.解决办法是什么?

小智 11

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

如果浏览器支持地理定位,并且如果getCurrentPosition成功运行,则调用成功函数.然后在功能successFunction


function successFunction(position) {
    var lat = position.coords.latitude;
    var long = position.coords.longitude;
    console.log('Your latitude is :'+lat+' and longitude is '+long);
}
Run Code Online (Sandbox Code Playgroud)

在此处阅读Geolocation API的更多信息