Android中用Google Map API出现的getLastKnowLocation空指针异常

这个问题也困扰我很久,为什么之前用模拟器测试有时可以成功有时却失败,而使用真机测试却从来没有成功,很是郁闷

首先大家应该记得下面一段代码的作用:

//设置服务商的信息 Criteria criteria =new Criteria(); //提供服务的精度标准 criteria.setAccuracy(Criteria.ACCURACY_FINE); //不需要高度信息 criteria.setAltitudeRequired(false); //不需要方位信息 criteria.setBearingRequired(false); //不允许产生费用 criteria.setCostAllowed(false); //消耗电力为低 criteria.setPowerRequirement(Criteria.POWER_LOW); //取得最匹配的criteria //best_provider=mlocationManager.getBestProvider(criteria, true);

通过上面的程序我们获得了最佳的服务提供方式,可是经过我多次实验发现每次获得的最佳提供者都是GPS方式,这就意味之后我们操作获取位置时使用过GPS方式获取的,那么接着我们通常是如下面方式获取位置的

//得到之前获得的坐标相关的信息 mLocation=mlocationManager.getLastKnownLocation(best_provider); //得到地址编码,用于后面查找当前地址名称 mGeocoder = new Geocoder(this,Locale.getDefault()); //更新位置信息 updateLocation(mLocation); //创建LocationManager对象,监听位置更改事情,并更新MapView mlocationManager.requestLocationUpdates(best_provider,6000,100,mLocationListener);

//更新定位 public void updateLocation(Location location) { if( location == null ) { return; //mlocationManager.requestLocationUpdates(best_provider,60000,1,mLocationListener); } //设置使用LocationOverlay来绘制当前位置 mLocationOverlay=new LocationOverlay(this); List<Overlay> overlays=mMapView.getOverlays(); overlays.add(mLocationOverlay); //绘制当前位置 mLocationOverlay.setLocation(location); //当前经纬度 Double geoLat=location.getLatitude()*1E6; Double geoLng=location.getLongitude()*1E6; //将其转换为GeoPoint型 GeoPoint point=new GeoPoint(geoLat.intValue(),geoLng.intValue()); //动态定位到指定坐标 mMapController.animateTo(point); } //设置位置监听器 private final LocationListener mLocationListener=new LocationListener() { //当位置发生变化时更新 public void onLocationChanged(Location location) { updateLocation(location); } //当Provider不可用时,比如GPS没有开启 public void onProviderDisabled(String provider) { updateLocation(null); } //当Provider启用时,比如GPS开启 public void onProviderEnabled(String provider){} //当Provider的状态改变时,比如在可以,暂时不可以用,关闭之间切换时 public void onStatusChanged(String provider,int status,Bundle extras){} };

注意上面的getLastKnownLocation方法,这个方法的意思是获取上一次知道的位置,这里问题是如果我们是第一次启动这个程序,那么上一次的位置是多少呢?我也很纳闷

我们知道在模拟器里面是使用geo fix 122 22这样的命令来模拟当前的位置的,我发现每次模拟位置的时候,第一次启动程序都会出错,而后面启动才获取到了位置,这也许就是那个方法的弊端吧,不知道自己有没有理解错误。

那么在真机上测试,这样行的通吗?和可惜,每次启动真机程序使用GPS来定位的时候,GPS模块已经打开,使用其他定位软件也可以找到当前的位置,可是自己写的那个程序却不能获取到位置。

参考网友的建议如:

while(location == null) { mlocationManager.requestLocationUpdates("best_provider", 60000, 1, locationListener); }

有网友写了下面的程序来替代上面的循环:

/** * 这里一定要对LocationManager进行重新设置监听 * mgr获取provider的过程不是一次就能成功的 * mgr.getLastKnownLocation很可能返回null * 如果只在initProvider()中注册一次监听则基本很难成功 */ @Override protected void onResume() { super.onResume(); mgr.requestLocationUpdates(bundle.getString("provider"), 60000, 1, locationListener); } @Override protected void onPause() { super.onPause(); mgr.removeUpdates(locationListener); }

上面的程序通过不断判断位置是否为空,直到获取到位置为止,他的解释是getLastKnownLocation一次不能成功,需要多次才能获取到,这刚好验证了我的猜想,之前每次第一次启动都不能成功的情况。可是我的那样做了除了一直循环外还是没有得到位置。

直到看到一个国外网页上这样获取位置,才知道是前面的那段服务提供的筛选条件使得我每次获取位置提供者的时候得到的结果都是GPS服务,而这样的方式并不见得好,后来我干脆把服务获取方式写成了直接使用网络方式获取

best_provider=mlocationManager.NETWORK_PROVIDER; //得到之前获得的坐标相关的信息 mLocation=mlocationManager.getLastKnownLocation(best_provider);

事实证明问题就出在位置服务获取方式上,改成使用网络方式后可以在真机上获取到当前位置。

那么我写的程序终于可以在真机上跑起来了,贴张图看看,呵呵

上面文字显示没有找到地址是因为网速的问题,本来应该通过地址编码找到相关位置的信息的,这是我做的一个小程序,使用Google Map API开发的,完成了些基本的地图功能,可以在上面做许多扩展的,目前LBS服务很火爆,什么UC乐园,大众点评,网易八方,街旁等,国外还有著名的foursquare,希望与有这方面兴趣的朋友一起学习交流。

下面是从国外网站上摘取的,解释getLastKnownLocation的使用方法:

The call to request update for a location is not blocking, hence it wont wait there. Also the provider in emulator may not have been started.

A possible check could be to see if the settings in it disable gps provider ? then send geo fix.

However, I would use Location Listener, it would be ideal in your case since you need a geo fix to proceed further.Location Listener is Used for receiving notifications from the LocationManager when the location has changed. You can unregister the listener after first geofix.

Note: It can take some time on device to get current location, and even on device this can return null.

下面是两个国外描述这方面问题的网页:

http://marakana.com/forums/android/examples/42.html

http://stackoverflow.com/questions/1608632/android-locationmanager-getlastknownlocation-returns-null

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值