【问题标题】:Geolocation in Phonegap using GPS使用 GPS 在 Phonegap 中进行地理定位
【发布时间】:2015-05-18 11:01:19
【问题描述】:

我正在使用 phonegap 应用程序中的地理定位。当高精度和省电模式打开时,我很容易获得位置。但是在 GPS 中工作时会出错。请帮忙。我被困在里面了。

我将超时设置为 60000 毫秒。但即使在那时也没有用。

代码是-

function Geolocation(){}

Geolocation.options = {
    maximumAge: 60000, 
    timeout: 15000, 
    enableHighAccuracy: true
};

Geolocation.errorMessage = 'Unable to get location data. Ensure that the location services are enabled in high accuracy mode. If enabled, try turning off/on location services and open this page again';

Geolocation.update = function() {
        navigator.geolocation.getCurrentPosition(Geolocation.updateSuccess, Geolocation.highAccuracyError, Geolocation.options);
}

Geolocation.highAccuracyError = function(err) {
    if (err.code == 3) {
        var options = Geolocation.options;
        options.enableHighAccuracy = false;
        options.timeout = 15000;

        navigator.geolocation.getCurrentPosition(Geolocation.updateSuccess, Geolocation.lowAccuracyError, options);
    }
}

Geolocation.lowAccuracyError = function(err) {
    Geolocation.currentLocation = null;
}

Geolocation.updateSuccess = function(position) {
    Geolocation.currentLocation = {};
    Geolocation.currentLocation.latitude = position.coords.latitude;
    Geolocation.currentLocation.longitude = position.coords.longitude;
}

提前致谢

【问题讨论】:

  • 您遇到的错误是什么?
  • 当我打开 GPS 而不是高精度或省电模式时,只会出现低精度错误。简而言之,如果只有 GPS 开启,即使 1 分钟后我也无法获得位置
  • 您的手机完全有可能无法仅根据 GPS 硬件获取其位置。例如,如果您在地下,您的手机将无法连接到 GPS 卫星。而且你不需要在地下就可以做到这一点。根据您所说,我认为 Cordova 正确地向您报告手机无法确定其位置。

标签: cordova gps geolocation


【解决方案1】:

@megha, GPS有两个问题。

1) 在 Android 上,您必须打开所有位置服务,包括 GPS 卫星、Google 的位置服务和快速 GPS。否则,您将收到该超时错误。

我认为谷歌故意破坏了它。以前不是这样的。

2) 使用 GPS 时,您必须处于运动状态 - 走动至少 20 英尺(即使是在一个圆圈内),并且您必须至少 3 颗卫星的现场线。大多数人通过在开放区域(例如足球场或停车场)来解决最后一个问题。

杰西

【讨论】:

  • 先生,非常感谢您的回答。当然,这些就是问题所在。但是,我想知道这个问题的真正解决方案是什么,因为我必须以编程方式捕获所有这些问题,因为我无法捕获用户的活动。
  • 您无法以编程方式修复您无法控制的问题。除了从 Phonegap 或物理获得的硬件之外,您无法控制硬件。 GPS需要一定数量的卫星。没有办法解决这个问题。大多数应用程序要求 GPS 一直打开,但这会耗尽电池电量。无论如何,除此之外的任何答案都不在这个论坛上。 --杰西
【解决方案2】:

您应该尝试使用watchPosition() 而不是getCurrentPosition()

getCurrentPosition() 在当前时间点对设备位置发出单一请求,因此在设备上的 GPS 硬件有机会获得定位之前,可能会发生位置超时。

我建议使用watchPosition() 来设置一个观察者,该观察者将在每次操作系统从 GPS 硬件接收到位置更新时调用成功函数。

因此,根据您当前的代码,类似于:

function Geolocation(){}

Geolocation.watchID;
Geolocation.minAccuracyInMetres = 50;

Geolocation.options = {
    maximumAge: 60000, 
    timeout: 15000, 
    enableHighAccuracy: true
};

Geolocation.errorMessage = 'Unable to get location data. Ensure that the location services are enabled in high accuracy mode. If enabled, try turning off/on location services and open this page again';

Geolocation.update = function() {
        Geolocation.watchID = navigator.geolocation.watchPosition(Geolocation.updateSuccess, Geolocation.highAccuracyError, Geolocation.options);
}

Geolocation.highAccuracyError = function(err) {
    if (err.code == 3) {
        var options = Geolocation.options;
        options.enableHighAccuracy = false;
        options.timeout = 15000;

        navigator.geolocation.getCurrentPosition(Geolocation.updateSuccess, Geolocation.lowAccuracyError, options);
    }
}

Geolocation.lowAccuracyError = function(err) {
    Geolocation.currentLocation = null;
}

Geolocation.updateSuccess = function(position) {
    // Reject if accuracy is not sufficient
    if(position.coords.accuracy > Geolocation.minAccuracyInMetres){
      return;        
    }

    // If only single position is required, clear watcher
    navigator.geolocation.clearWatch(Geolocation.watchID);

    Geolocation.currentLocation = {};
    Geolocation.currentLocation.latitude = position.coords.latitude;
    Geolocation.currentLocation.longitude = position.coords.longitude;
}

【讨论】:

    【解决方案3】:

    您的手机完全有可能无法仅根据 GPS 硬件获取其位置。例如,如果您在地下,您的手机将无法连接到 GPS 卫星。而且你不需要在地下就可以做到这一点。根据您的说法,我认为 Cordova 正确地向您报告手机无法确定其位置。

    【讨论】:

      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2014-04-08
      • 1970-01-01
      相关资源
      最近更新 更多