地理定位 运行 在 Android 上非常慢

Geolocation running very slow on Android

我正在使用 Ionic/Angular/Firebase 构建应用程序。

该应用程序需要用户的地理定位。我正在使用 Ionic native 的地理定位插件,但我遇到了 Android 的一些速度问题。

这是 Ionic Native 的问题吗? Android有没有专门的插件?

这是我的代码:

this.geolocation.getCurrentPosition({enableHighAccuracy: false, timeout: 3000, maximumAge: 100000}).then((resp) => {
   this.userLocation.lat = resp.coords.latitude;
   this.userLocation.lng = resp.coords.longitude;
   this.getLocation();
}).catch((error) => {
  this.geolocation.getCurrentPosition({enableHighAccuracy: true, timeout: 3000, maximumAge: 100000}).then((resp) => {
     this.userLocation.lat = resp.coords.latitude;
     this.userLocation.lng = resp.coords.longitude;
     this.getLocation();
  }).catch((error) => {
    this.userLocation.lat = this.userData.location.lat || 0;
    this.userLocation.lng = this.userData.location.lng || 0;
    //if fails, get users last known location.
    this.getLocation();
    console.log('Error getting location', error);
  });
  console.log('Error getting location', error);
});

我无法想象这段代码是错误的。正如我所说,它在 PC 和 iOS 上运行得非常好。如果用户在 android 上,我应该做些什么?我可以使用其他插件吗?任何帮助都会很棒!

代码没有问题。我在 android 手机中也遇到了同样的问题。 我试过的解决方案。

手动解决

  • 进入位置设置选项。检查定位模式是否not设置为Device Only。将其设置为高精度,然后尝试
  • OFF 并打开您的位置设置选项进入设置-> 位置选项。重新启动您的 Phone 并尝试

  • 检查 google 地图是否正在获取您的当前位置。尝试在 google 地图上获取您当前的位置。无论如何,如果 google 地图找不到您的当前位置,那么您将无法在您的应用中获取位置信息。

一旦您在 google 地图上找到当前位置,您就可能将该位置输入到您的应用程序中。

使用插件解决

<plugin name="cordova-plugin-request-location-accuracy" spec="*" /> 

您使用的代码有效。

@Maheshvirus 所说的是“cordova-plugin-request-location-accuracy”插件,它实际上允许您提示用户为 GPS 打开高定位精度(并检查它是否首先打开),这将 'Use GPS, WiFi, Bluetooth, or cellular networks to determine location'。我在我的应用程序中使用相同的插件,效果很好。

通过快速谷歌搜索,IOS 似乎没有高精度模式,因为默认情况下 IOS 使用 gps、wifi、蓝牙等来获取您的位置...您不能像 Android 一样打开和关闭它,这解释了为什么 IOS 总是那么快知道你在哪里。因此,在 Android 设备上,您需要提示用户使用@Maheshvirus 提到的插件打开它。然后它应该始终保持打开状态,除非用户将其模式更改为省电或仅设备定位模式。

PC 应该有更快的互联网并且不应该有数据上限或严格的节流(除非网络中立性现在完全消失或者你在其他一些可怕的地方)所以你的浏览器应该能够很容易地找到你并快速通过您所连接的 modem/router - 有关详细信息,请参阅 this post answer

解决方案

我现在太累了,无法为您提供所需的工作代码示例,但是 利用 Geolocation.watchPosition() 而不是 getCurrentPosition() 可以让您更快地获取位置 根据我的使用经验 - this user seems to have tested it。 watchPosition 'is used to register a handler function that will be called automatically each time the position of the device changes'。 watchPosition 函数的使用几乎与 getCurrentPosition 完全相同,除了在调用它时您必须为其分配一个 ID 并在完成时使用 Geolocation.clearwatch(idname) 。 Here is the docs for watchPosition()。所以很酷的事情是,如果用户移动或电话定位服务认为他们已经移动了,它就不必启动另一个对设备 GPS 的调用(这就是导致它花费这么长时间的原因),因为 GPS 已经聆听。

抱歉,我没有提供实际的代码修复,但希望这些详细信息对您有所帮助。当我在不同的设备上使用 getCurrentPosition 时,这也是我 运行 遇到的一个问题。 您实施 watchPosition() 应该没有问题,并且应该会立即看到 android 定位延迟改进 - 特别是如果您让用户使用该插件打开高精度定位模式 ' cordova-plugin-request-location-accuracy'

我和 Geolocation 有过类似的情况。仅在 Platform 准备就绪后才调用本机方法至关重要。虽然这是您应该如何称呼它,但忽略它会导致在您从插件获得响应之前出现明显的延迟,在 Android.

中更是如此
import { Platform } from 'ionic-angular'
...

constructor( private platform: Platform) { 
this.platform.ready().then(() => {
  //request location here
 })
}
...

我只是在选项中删除了 maximumAge。我的最终选择:

 { enableHighAccuracy: true, timeout: 10000 }