android位置更新频率 [英] android location update frequency

查看:132
本文介绍了android位置更新频率的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我想获得5个连续的更新位置,以便检查用户是否在移动并确保GPS已校准.我做了以下事情:

I want to get 5 consecutive update locations in order to check if the user is moving and to make sure the GPS is calibrated. I did the following:

我在清单和onConnected中添加了android.permission.ACCESS_FINE_LOCATION"

I added android.permission.ACCESS_FINE_LOCATION" to the manifest and in onConnected:

mLocationRequest = new LocationRequest();
mLocationRequest.setPriority(LocationRequest.PRIORITY_HIGH_ACCURACY);
mLocationRequest.setInterval(1000); // Update location every second
mLocationRequest.setFastestInterval(1000);
LocationServices.FusedLocationApi.requestLocationUpdates(mGoogleApiClient, mLocationRequest, this); 

和在onLocationChanged中,我执行了以下操作:

and in onLocationChanged I did the following:

locationRetries++;
switch (locationRetries) {
case 1: {
    firstLatitude = location.getLatitude();
    firstLongitude = location.getLongitude();               
}
case 5: {
    locationRetries = 0;
    lastLatitude = location.getLatitude();
    lastLongitude = location.getLongitude();
    accuracy = Math.round(location.getAccuracy());
    stopLocationUpdates();//will remove from location updates and disconnects
    float[] results = new float[1];
    Location.distanceBetween(firstLatitude, firstLongitude, lastLatitude, lastLongitude, results);              
    if (results[0] > 5)... //if moved at least 5meters, show popup that the user is moving else do your thing
}

现在我有3个问题:

1)即使我将两个参数都设置为1000毫秒,似乎要花费不到5秒的时间来检索5个更新.

1) It seems to take much less than 5 seconds to retrieve the 5 updates even though I set both parameters to be 1000 milliseconds.

2)即使我走得很快,所有5个位置(至少第一个和最后一个)都是相同的精确位置.我以为我可能移动得太慢了,所以

2) All the 5 locations (at least the first and last ones) are the same exact location even though I was walking fast. I thought I maybe moving too slow so

3)我关闭了我的应用程序,在很远的地方重新打开了它,然后按下了按钮.几乎立即我就到达了先前的位置.我再次按下按钮,然后得到了我所在的真实位置.好像并没有真正从GPS请求/等待某个位置,而是选择了当时当时不太准确的最后一个.我没有任何获取最近的位置"代码.

3) I closed my app, reopened it on a far location and pressed the button. Almost instantly I got the previous location. I pressed the button again and then I got the real location I was on. It's as if it didn't really asked/waited for a location from the GPS but instead took the last one which was remotely inaccurate at the time. I don't have any "get last known location" code.

我猜想最重要的是:当我询问位置以及连续5次询问时,如何确保它真正询问GPS我在哪里,而不是从我那里得到真实的位置缓存(?).

I guess the bottom line would be: how can I make sure that it really asks the GPS where am I when I asked for the location and also when asking it for 5 consecutive times, to give me the real locations and not from the cache(?).

推荐答案

已融合位置提供者

智能地管理基础定位技术,并根据您的需求为您提供最佳位置.

intelligently manages the underlying location technology and gives you the best location according to your needs.

  • 简单的API:您可以指定高层需求,例如高精度"或低功耗",而不必担心位置提供程序.
  • 立即可用:使您的应用立即访问最近的最佳位置.
  • 电源效率:最大限度地减少应用程序的电源使用量. 基于所有传入的位置请求和可用的传感器,融合的位置提供者选择最有效的方式来满足这些需求.
  • 多功能性:满足广泛的需求,从需要高度精确位置的前台使用到需要定期位置更新且功耗影响可忽略的后台使用.
  • Simple APIs: Lets you specify high-level needs like "high accuracy" or "low power", instead of having to worry about location providers.
  • Immediately available: Gives your apps immediate access to the best, most recent location.
  • Power-efficiency: Minimizes your app's use of power. Based on all incoming location requests and available sensors, fused location provider chooses the most efficient way to meet those needs.
  • Versatility: Meets a wide range of needs, from foreground uses that need highly accurate location to background uses that need periodic location updates with negligible power impact.

不能保证融合的位置提供者将使用GPS-如果它具有您要求的精度的最新位置,它将一直返回直到返回更好的位置(即实时GPS返回准确的位置).这样可以确保您更快地获得一个更好的位置,而无需等待GPS灌注.

There is no guarantee that the fused location provider will ever use GPS - if it has a recent location of the accuracy you request it will return until a better location is returned (i.e., live GPS is returning accurate locations). This ensures that you'll get a better location sooner without waiting for GPS to be primed.

如果您特别需要GPS数据,则需要使用 LocationManager 使用 GPS_PROVIDER .

If you specifically need data from GPS, you need to use the LocationManager using the GPS_PROVIDER.

如果您要确定用户当前正在做什么,则可以改用

If you are trying to determine what the user is currently doing, you can instead use the ActivityRecognitionApi, which returns DetectedActivity such as WALKING or STILL: using that would give a faster method to understand what the user is currently doing.

这篇关于android位置更新频率的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆