(二)进阶练习____1、让你的App可定位——获取当前位置-Obtaining the Current Location
负责人:walker02
原文链接:http://docs.eoeandroid.com/training/basics/location/currentlocation.html
目录[隐藏] |
Obtaining the Current Location
通过LocationManager设置好你的应用之后,你就可以获取位置数据更新了。
设置位置监听
LocationManager类拥许多方法用于接受位置数据更新,在它里面最简单的形式,注册一个时间监听,用于识别来自位置数据更新的位置管理,以及设置最小时间和间隔距离。随着时间和位置间隔的更新,回调函数onLocationChanged()会被调用。 在下边的代码中,每隔至少十秒或者距离移动了十米,位置监听函数将要被调用。另外一个通知应用改变状态的回调函数来自位置提供者。
private final LocationListener listener = new LocationListener() { @Override public void onLocationChanged(Location location) { // A new location update is received. Do something useful with it. In this case, // we're sending the update to a handler which then updates the UI with the new // location. Message.obtain(mHandler, UPDATE_LATLNG, location.getLatitude() + ", " + location.getLongitude()).sendToTarget(); ... } ... }; mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 10000, // 10-second interval. 10, // 10 meters. listener);
处理多元位置更新
通常来说,使用GPS的位置提供者要比基于网络的位置提供者耗费更多的时间。如果你尽可能快的更新位置数据以及你想让更精确的更新数据变得可能,常见的方法就是注册GPS和网络提供者两种方式的监听器。在onLocationChanged()回调函数里,你将接收来自多个位置提供的的信息,包含带有不同时间戳和不同等级的信息。你需要通过逻辑处理来消除歧义信息以及更新最新数据。下边的代码片段演示了这段逻辑。
private static final int TWO_MINUTES = 1000 * 60 * 2; /** Determines whether one Location reading is better than the current Location fix * @param location The new Location that you want to evaluate * @param currentBestLocation The current Location fix, to which you want to compare the new one */ protected boolean isBetterLocation(Location location, Location currentBestLocation) { if (currentBestLocation == null) { // A new location is always better than no location return true; } // Check whether the new location fix is newer or older long timeDelta = location.getTime() - currentBestLocation.getTime(); boolean isSignificantlyNewer = timeDelta > TWO_MINUTES; boolean isSignificantlyOlder = timeDelta < -TWO_MINUTES; boolean isNewer = timeDelta > 0; // If it's been more than two minutes since the current location, use the new location // because the user has likely moved if (isSignificantlyNewer) { return true; // If the new location is more than two minutes older, it must be worse } else if (isSignificantlyOlder) { return false; } // Check whether the new location fix is more or less accurate int accuracyDelta = (int) (location.getAccuracy() - currentBestLocation.getAccuracy()); boolean isLessAccurate = accuracyDelta > 0; boolean isMoreAccurate = accuracyDelta < 0; boolean isSignificantlyLessAccurate = accuracyDelta > 200; // Check if the old and new location are from the same provider boolean isFromSameProvider = isSameProvider(location.getProvider(), currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and accuracy if (isMoreAccurate) { return true; } else if (isNewer && !isLessAccurate) { return true; } else if (isNewer && !isSignificantlyLessAccurate && isFromSameProvider) { return true; } return false; } /** Checks whether two providers are the same */ private boolean isSameProvider(String provider1, String provider2) { if (provider1 == null) { return provider2 == null; } return provider1.equals(provider2); }
更明智的使用getLastKnowLocation()
设置时间用于合理的位置修改可能不被特定的某个应用所接收,你应该考虑使用getLastKnownLocation()方法,这个方法是查询Android最新更新数据依赖于位置提供者。记着返回的数据不一定是最新的。你应该检查时间戳和位置精度,自己决定是否在你的应用中使用。你过你丢弃来自getLastKnownLocation()返回的数据,等待来自位置提供者返回的数据,你应该考虑的是在位置数据接收之前来显示合适的信息。
终止位置更新
当你完成了使用未知数据的时候,你应该终止位置数据的更新来减少不必要的电量和流量的浪费。如果用户离开了当前显示位置数据的activity,你应该在onStop()里调用removeUpdates()来停止位置更新。(onStop()是在activity不在可见时候被调用,如果你想了解activity的生命周期,你可以参考停止和重启一个activity的课程)
protected void onStop() { super.onStop(); mLocationManager.removeUpdates(listener); }
注意:对于需要不断接受和处理数据的应用程序,比如一个实时图像映射的应用,你最好将位置更新的逻辑放在后台服务中进行处理。使用系统提示条提示用户位置数据正在被使用。

浙公网安备 33010602011771号