实现Android的不同精度的定位(基于网络和GPS)[转]
如何开启位置服务的监听,如何停止监听,如何获得不同精度的定位,以及如何判断定位是否更精确。
Android中的定位服务的相关类基本上都在android.location包中,下面会按编写的顺序依次讲解。
位置服务管理器(LocationManager)
首先,我们需要1个LocationManager,考虑到它会被多个方法使用,我们将它定义成Activity的Field。然后在onCreate方法中为它赋值。
//变量定义private LocationManager locationManager;//得到LocationManagerlocationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE);开启位置服务的监听
private class MyLocationListner implements LocationListener{@Overridepublic void onLocationChanged(Location location) {// Called when a new location is found by the location provider.Log.v("GPSTEST", "Got New Location of provider:"+location.getProvider());if(currentLocation!=null){if(isBetterLocation(location, currentLocation)){Log.v("GPSTEST", "It's a better location");currentLocation=location;showLocation(location);}else{Log.v("GPSTEST", "Not very good!");}}else{Log.v("GPSTEST", "It's first location");currentLocation=location;showLocation(location);}//移除基于LocationManager.NETWORK_PROVIDER的监听器if(LocationManager.NETWORK_PROVIDER.equals(location.getProvider())){locationManager.removeUpdates(this);}} //后3个方法此处不做处理public void onStatusChanged(String provider, int status, Bundle extras) {} public void onProviderEnabled(String provider) {} public void onProviderDisabled(String provider) {}};Location currentLocation;private void showLocation(Location location){//纬度Log.v("GPSTEST","Latitude:"+location.getLatitude());//经度Log.v("GPSTEST","Longitude:+location.getLongitude());//精确度Log.v("GPSTEST","Accuracy:"+location.getAccuracy());//Location还有其它属性,请自行探索} private LocationListener gpsListener=null;private LocationListener networkListner=null;private void registerLocationListener(){networkListner=new MyLocationListner();locationManager.requestLocationUpdates(LocationManager.NETWORK_PROVIDER, 3000, 0, networkListner);gpsListener=new MyLocationListner();locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 5000, 0, gpsListener);} 以上的代码还是很易懂的吧,创建1个监听器对象,然后指定1个provider,然后requestLocationUpdates。在监听器中检查如果是NETWORK_PROVIDER,则取消监听,只留GPS的监听(在你的实际应用中可以根据情况来进行,因为GPS有可能会因为建筑的阻挡而暂时不工作)。 private static final int CHECK_INTERVAL = 1000 * 30;protected boolean isBetterLocation(Location location,Location currentBestLocation) {if (currentBestLocation == null) {// A new location is always better than no locationreturn true;} // Check whether the new location fix is newer or olderlong timeDelta = location.getTime() - currentBestLocation.getTime();boolean isSignificantlyNewer = timeDelta > CHECK_INTERVAL;boolean isSignificantlyOlder = timeDelta < -CHECK_INTERVAL;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 movedif (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 accurateint 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 providerboolean isFromSameProvider = isSameProvider(location.getProvider(),currentBestLocation.getProvider()); // Determine location quality using a combination of timeliness and// accuracyif (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);} if(gpsListener!=null){locationManager.removeUpdates(gpsListener);gpsListener=null;} LocationManager的其它使用