【问题标题】:Geolocation doesn't work with GPS off地理定位在 GPS 关闭的情况下不起作用
【发布时间】:2015-01-13 09:31:08
【问题描述】:

我正在使用 phonegap 构建服务编写一个带有 htlm5 和 javascript 的移动应用程序。 我尝试获取地理位置,但如果我的智能手机上的 GPS 关闭,它就不起作用。大约 30 秒后,我收到 alert("Sorry, no position available.") 警报。 如果我打开gps,我就会得到地理位置。 我试过getCurrentPositionwatchPosition 都不适合我。 我的代码:

document.addEventListener("deviceready", onDeviceReady, false);

var geo_options = {
    enableHighAccuracy : true,
    maximumAge : 30000,
    timeout : 27000
};

function onDeviceReady() {
    // navigator.geolocation.getCurrentPosition(geo_success, geo_error, geo_options);
    navigator.geolocation.watchPosition(geo_success, geo_error, geo_options);
}

function geo_success(position) {
    alert(position.coords.latitude+" "+position.coords.longitude);
}

function geo_error() {
    alert("Sorry, no position available.");
}

当智能手机上的 GPS 关闭时,我如何获取地理位置?

【问题讨论】:

  • 问题是什么?目前,只有陈述,都是正确的。
  • @RoryMcCrossan 我该怎么做?
  • 您需要让用户在 UI 中启用定位服务。您无法通过代码启用它。
  • 您可以检查 if(navigator.geolocation) {...} 来处理这两种情况。
  • 哦,谢谢@JamesWaddington。你帮了我。回答我,我会接受。 :)

标签: javascript jquery cordova gps geolocation


【解决方案1】:

您可以使用检查来查看地理位置是否可用,以便您可以处理这两种情况:

if (navigator.geolocation) {
// get location
} else {
// handle no geolocation
}

【讨论】:

    猜你喜欢
    • 2019-07-02
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多