gpt4 book ai didi

android - 如何阻止用户长时间等待位置修复?

转载 作者:塔克拉玛干 更新时间:2023-11-02 23:27:49 24 4
gpt4 key购买 nike

我正在构建一个 GPS Android 应用程序,它根据用户的当前位置获取最近的地点。

这是我的应用程序所做的:

  • 检查 GPS 或网络是否可用
  • 如果两者都不可用,则什么都不做。否则,我们首先检查 GPS 是否存在,如果不存在则检查网络。
  • 使用其中之一后,我们获取当前位置,然后将其发送到服务器。
  • 从服务器检索数据并更新 UI 后,我们将停止监听进一步的位置更新。一次就够了,直到他们按下刷新按钮再次开始。

我希望做的事情:

  • 如果 GPS 或网络无法检索位置(例如 2 分钟),那么我们会更换提供商。我们不希望用户等待太久。

  • 如果能够同时使用这两个提供程序,然后从中获得最准确的信息,那就太好了。我看过http://developer.android.com/guide/topics/location/strategies.html我看到了 isBetterLocation 方法。我如何将此方法集成到我的应用程序中?我无法理解应该如何、在何处以及何时调用它。我假设 isBetterLocation() 要求我同时调用网络和 GPS。如果我的应用程序能够同时收听两者以确保准确性,那就太好了。我该怎么做呢?如果其中之一不可用怎么办?

这是我的部分代码:

if(!GPSEnabled && !networkEnabled)
{
Toast.makeText(this, "Error: This application requires a GPS or network connection",
Toast.LENGTH_SHORT).show();
}
else
{
if(GPSEnabled)
{
locManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
}
else if(networkEnabled)
{
System.out.println("Getting updates from network provider");
locManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 0, 0, this);
}
}

这是 onLocationChanged 方法。我得到纬度/经度值,然后将它们发送到我的服务器,然后用它做适当的事情。

public void onLocationChanged(Location location)
{
//Get coordinates
double lat = (location.getLatitude());
double lng = (location.getLongitude());
Log.d("MainActivity", "got location: " + lat + ": " + lng);
//get nearest locations
new GetLocations().execute(SharedVariables.root + SharedVariables.locationsController + SharedVariables.getNearestMethod + lat + "/" + lng);

// Zoom in, animating the camera after the markers have been placed
map.animateCamera(CameraUpdateFactory.newLatLngZoom(new LatLng(lat, lng), 10));
System.out.println("lat = " + lat + ", lng = " + lng);

//Stop listening for updates. We only want to do this once.
locManager.removeUpdates(this);
}

最佳答案

无论是使用 GPS 还是网络,您都无法大幅减少获取位置的最终等待时间。

这两种定位方法各有优缺点:

  • GPS 通常需要较长时间才能知道您的位置,因为寻找和校准 GPS 卫星是一项艰巨的任务(需要至少找到 3 颗正确对齐的卫星)。最重要的是,添加最终阻碍信号的障碍物/环境。除非您在应用程序启动之前/启动时启用 GPS,否则必须等待。 另一方面,您会得到一个细粒度的位置。

  • 如果您只需要一个粗略的位置,使用网络是一种解决方案:它会在几秒钟内返回数据。 但是位置的精确度因可用/使用的网络而异;我没有任何实际示例,但我猜精度可能很大变化。 GPS 将总是比网络更精确的位置。

了解没有预定义的选择。一切都取决于您的应用如何处理这些数据。

如果可用,则使用 GPS,同时向网络发出位置请求,如果没有 GPS 设备,则回退到网络。然后采取:

  • 如果您只需要一个位置,则为第一个结果
  • 如果您想要最精确的数据,这是首选结果

If GPS or network fails to retrieve a location, for example, 2 minutes, then we switch providers. We don't want the user to wait too long.

在这两种情况下,都不要设置回退计时器:您只会在第一个提供者失败的情况下延长等待时间;所以两个请求同时进行,除非 android API 不允许异步位置请求。

关于android - 如何阻止用户长时间等待位置修复?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/15312052/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com