从navigator.geolocation.getCurrentPosition返回值到外部函数

时间:2018-09-11 16:33:37

标签: javascript geolocation

我有以下代码片段,试图在其中返回从 navigator.geolocation.getCurrentPosition 获得的地理位置坐标,如下所示:

function getCurrentPos() {
    var current_pos = {};
    // Try HTML5 geolocation.
    if (navigator.geolocation) {
        navigator.geolocation.getCurrentPosition(function(position) {
            current_pos = {
                lat: position.coords.latitude,
                lng: position.coords.longitude
            };
        }, showGeolocationError);
    } else {
        // Browser doesn't support Geolocation
        alert('Error: The Geolocation service failed. : Your browser doesn\'t support geolocation.');
    }

    return current_pos;
}

但是 current_pos 的值仍然返回空对象。调用自定义 getCurrentPos 函数时如何获得其价值?

感谢您的帮助

0 个答案:

没有答案