【问题标题】:navigator.geolocation.getCurrentPosition(position) getting error "position is not defined" [duplicate]navigator.geolocation.getCurrentPosition(position) 出现错误“未定义位置”[重复]
【发布时间】:2018-04-07 13:08:43
【问题描述】:

在我的应用程序中需要获取当前位置,我尝试使用下面的代码,我得到了位置未定义之类的错误。获取 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。

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

【问题讨论】:

  • 获取 API 对象“位置”本身的错误未定义。

标签: javascript jquery google-maps


【解决方案1】:

正如您的错误所述,使用此功能仅适用于安全连接(通过 https 提供服务)。阅读this 以供参考。

【讨论】:

    猜你喜欢
    • 2018-12-29
    • 2016-06-29
    • 1970-01-01
    • 2016-04-17
    相关资源
    最近更新 更多