navigator.geolocation.getCurrentPosition(position)获取错误"位置未定义"

时间:2018-04-07 13:08:43

标签: javascript jquery google-maps

在我的应用程序需要获取当前位置,我已尝试使用下面的代码,我收到错误,如位置未定义。错误是获取API对象'位置'本身没有定义。

try {
    if (event.handled !== true) {
        event.handled = true;
        if (navigator.geolocation) {
            var geoOptions = { enableHighAccuracy: true, timeout: 20000 };
            navigator.geolocation.getCurrentPosition(function (position) { // geoSuccess
                $("#clientPersonalDetailesDiv #longitude").val(position.coords.longitude);
                $("#clientPersonalDetailesDiv #latitude").val(position.coords.latitude);
                $.hcmobile.hideLoader();
            }, function (error) { // geoError
                $.hcmobile.hideLoader();
                if (error.code == '3') {
                    $.hcmobile.alertBox("ErrorMessage", "GPS is not available on this device");
                }
                else {
                    $.hcmobile.alertBox("ErrorMessage", error.message + 'You must turn on location services to use this feature');
                    //$.hcmobile.alertBox("ErrorMessage", "You must turn on location services to use this feature");
                }
            }, geoOptions);
        }
    }
}
catch (e)
{
}

我收到错误代码1,它说:

  

getCurrentPosition()和watchPosition()不再适用于不安全的起源。要使用此功能,您应该考虑将应用程序切换到安全的来源,例如HTTPS。

我附上了屏幕截图以获取更多信息。

screenshot

1 个答案:

答案 0 :(得分:0)

使用此功能仅适用于安全连接(通过https提供),因为您的错误已经说明了。请阅读this以供参考。