如何使用google map api在php中获取当前的纬度和经度?

Dee*_*ini 2 javascript php google-maps

如何使用google map api在php中获取当前的纬度和经度?我正在尝试使用 HTML5 获取纬度和经度,但我无法获取函数之外的变量。请分享一些关于此的知识。

$(document).ready(function () {
    var latitude = ''; var longitude = '';

    if(navigator.geolocation) {
        browserSupportFlag = true;
        navigator.geolocation.getCurrentPosition(function(position) {
         latitude = position.coords.latitude;
         longitude = position.coords.longitude;
        }, function() {
          alert("Geolocation Failed");
        });
    }
    console.log(longitude); // I am trying to console this variable here but its print blank;
});
Run Code Online (Sandbox Code Playgroud)

Mat*_*Mat 5

您应该在成功回调中移动控制台日志行:https : //developer.mozilla.org/en-US/docs/Web/API/Geolocation/getCurrentPosition

换句话说,您必须等待 getCurrentPosition 的执行给您一个结果。