这两天可憋坏我了,一直愁没什么题材可以充实我的博客,正巧最近遇到一个比较棘手的问题:使用GPS定位无法获取当前的地理位置,即getLastKnownLocation方法始终返回null。
后来一篇博文getLastKnownLocation()返回null的解决帮了我大忙,在此对该博客作者表示感谢,但是有几点需要注意的,我觉得有必要补充一下,否则看了这篇博文也还是得不到当前的地理位置。
第一:当使用GPS定位时,最好不要使用getLastKnownLocation方法获得当前位置对象Location,因为该对象可以在onLocationChanged的参数中由系统给予(根据文档,getLastKnownLocation仅仅是获取当缓存中的上一次打开地图缓存起来的位置)。这样就避免了空指针异常。而且更重要的是GPS定位不是一下子就能定位成功的,在90%以上的情况下,getLastKnownLocation返回null
第二:LocationListener最好在Activity的onCreate()方法中进行实例化实现系统的回调方法:onLocationChanged(finalLocationloc)onProviderDisabled(finalStrings)onProviderEnabled(finalStrings)onStatusChanged(finalStrings,finalinti,finalBundleb)
第三:requestLocationUpdates必须要在onResume()中进行注册监听.且在onPause()中进行反注册。
第四:测试GPS是否定位成功,去一个空旷的地方去,不要有遮挡。这点非常重要,不然,你永远也不知道自己GPS定位是否成功。
以下是我用GPS成功获取当前地理位置的例子。希望能够帮助大家摆脱GPS定位的阴霾。
@OverridepublicvoidonCreate(finalBundleicicle){
super.onCreate(icicle);
this.setContentView(R.layout.activity_mapview);
mBtnDone=(Button)findViewById(R.id.btn_done);
mBtnDone.setOnClickListener(this);
mapView=(MapView)findViewById(R.id.map_view);
mapView.setBuiltInZoomControls(true);
mapController=mapView.getController();
mLocationManager=(LocationManager)getSystemService(Context.LOCATION_SERVICE);
mDefaultMarker=getResources().getDrawable(R.drawable.map_redpin);
mDefaultMarker.setBounds(0,0,mDefaultMarker.getIntrinsicWidth(),mDefaultMarker.getIntrinsicHeight());
mBuoyOverlay=newBuoyItemizedOverlay(mDefaultMarker,this);
initDensityDpi();
mZoomLevel=mapView.getMaxZoomLevel()-1;
//LocationListener最好在Activity的onCreate()方法中进行实例化,当GPS获得Location时,会自动调用onLocationChanged方法.
mLocationListener=newLocationListener(){
@Override
publicvoidonLocationChanged(finalLocationloc){
LogHelper.i(TAG,"onLocationChanged.loc:"+loc);
if(loc!=null){
LogHelper.i(TAG,"onLocationChanged.latitude:"
+loc.getLatitude()+",longtitude:".getLongitude());
GeoPointgeoPoint=MapUtils.getGeoPoint(loc);
mapController.animateTo(geoPoint);
initBuoyOverlayItems(loc);
}else{
Toast(MapViewActivity.this,"Yourcurrentlocationistemporarilyunavailable.",Toast.LENGTH_SHORT).show();
}
}
//当系统Setting->Location&Security->Usewirelessnetworks取消勾选,UseGPSsatellites取消勾选时调用
publicvoidonProviderDisabled(finalStrings){
LogHelper.i(TAG,"onProviderDisabled.");
}
//当系统Setting->Location&Security->Usewirelessnetworks勾选,UseGPSsatellites勾选时调用
publicvoidonProviderEnabled(finalStrings){
LogHelper.i(TAG,"onProviderEnabled.");
}
publicvoidonStatusChanged(finalStrings,finalinti,finalBundleb){
LogHelper.i(TAG,"onStatusChanged.");
}
};
}
@OverridepublicvoidonStart(){
super.onStart();
mapController.setZoom(mZoomLevel);
if(!DoSomeGoodUtils.isNetworkAvailable(this)){
mBtnDone.setEnabled(false);
showDialog(DIALOG_NO_NETWORK);
}else{
//判断UseGPSsatellites.是否勾选
booleanisGpsEnabled=MapUtils.isGPSProviderAvaliable(this);
//判断Usewirelessnetworks是否勾选
booleanisWIFIEnabled=MapUtils.isWIFIProviderAvaliable(this);
if(!isGpsEnabled&&!isWIFIEnabled){
如果都没有勾选,则弹出对话框,提示用户勾选。
}else{
LocationlastKnownLocation=null;
//如果只是UseGPSsatellites勾选,即指允许使用GPS定位
if(isGpsEnabled&&!isWIFIEnabled){
lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mProviderName=LocationManager.GPS_PROVIDER;
//如果只是Usewirelessnetworks勾选,即只允许使用网络定位。
}elseif(!isGpsEnabled&&isWIFIEnabled){
lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mProviderName=LocationManager.NETWORK_PROVIDER;
//如果二者都勾选,优先使用GPS,因为GPS定位更精确。
}elseif(isGpsEnabled&&isWIFIEnabled){
lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER);
mProviderName=LocationManager.GPS_PROVIDER;
if(lastKnownLocation==null){
lastKnownLocation=mLocationManager.getLastKnownLocation(LocationManager.NETWORK_PROVIDER);
mProviderName=LocationManager.NETWORK_PROVIDER;
}
}
if(!TextUtils.isEmpty(mProviderName)){
mLocationManager.requestLocationUpdates(mProviderName,1000,1,mLocationListener);}
//如果一下子就能定位成功,则执行以下代码,当用网络定位时,大都能一次性定位成功,当用GPS时,该代码不会起太大作用。
if(lastKnownLocation!=null){
mBtnDone.setEnabled(true);
//获取当前地理位置
GeoPointlastKnownPoint=getLastKnownPoint(lastKnownLocation);
//以动画方式移动到该地理位置
mapController.animateTo(lastKnownPoint);
//更新浮标。该方法在这里就不公开了。知道它的含义就行
initBuoyOverlayItems(lastKnownLocation);
}
}
}
}
@Override
protectedvoidonResume(){
super.onResume();
LogHelper.i(TAG,"onResume.ProviderName:"+mProviderName);
if(!TextUtils.isEmpty(mProviderName)){
//当GPS定位时,在这里注册requestLocationUpdates监听就非常重要而且必要。没有这句话,定位不能成功。
mLocationManager.requestLocationUpdates(mProviderName,1000,1,mLocationListener);
}
}
@OverrideprotectedvoidonPause(){
super.onPause();//取消注册监听
if(mLocationManager!=null){
mLocationManager.removeUpdates(mLocationListener);
}
}}