在玩一个GPS的东西 获取Location一直为null 后来是从onLocationChanged里面拿location才搞定了 贴一下吧 代码太乱
-
-
-
-
packagecom.jeedroid.tools;
-
-
importjava.io.IOException;
-
importjava.util.List;
-
importjava.util.Locale;
-
-
importandroid.content.Context;
-
importandroid.location.Address;
-
importandroid.location.Criteria;
-
importandroid.location.Geocoder;
-
importandroid.location.Location;
-
importandroid.location.LocationListener;
-
importandroid.location.LocationManager;
-
importandroid.os.Bundle;
-
importandroid.provider.Settings;
-
importandroid.util.Log;
-
-
publicclassLocationTools
- {
-
privateLocationlocation;
-
publicstaticLocationManagergetLocationManager(Contextcontext)
- {
-
return(LocationManager)context.getSystemService(context.LOCATION_SERVICE);
- }
-
-
publicStringgetAddress(Contextcontext)
- {
- LocationManagerlocationManager=this.getLocationManager(context);
-
if(!locationManager.isProviderEnabled(android.location.LocationManager.GPS_PROVIDER))
- {
- android.provider.Settings.Secure.setLocationProviderEnabled(context.getContentResolver(),LocationManager.GPS_PROVIDER,false);
- }
-
returndoWork(context);
- }
-
-
privateStringdoWork(Contextcontext)
- {
- Stringaddres="";
- LocationManagerlocationManager=this.getLocationManager(context);
- Criteriacriteria=newCriteria();
- criteria.setAccuracy(Criteria.ACCURACY_FINE);
- criteria.setAltitudeRequired(false);
- criteria.setBearingRequired(false);
- criteria.setCostAllowed(false);
- criteria.setPowerRequirement(Criteria.POWER_LOW);
- Stringprovider=locationManager.getBestProvider(criteria,true);
- Log.i("provider>>>>>>",provider);
-
while(location==null)
- {
- location=locationManager.getLastKnownLocation(provider);
- }
- LocationListenerlocationListener=newLocationListener()
- {
-
- @Override
-
publicvoidonLocationChanged(Locationlocation)
- {
- LocationTools.this.location=location;
- }
-
- @Override
-
publicvoidonProviderDisabled(Stringprovider)
- {
-
- }
-
- @Override
-
publicvoidonProviderEnabled(Stringprovider)
- {
-
- }
-
- @Override
-
publicvoidonStatusChanged(Stringprovider,intstatus,Bundleextras)
- {
-
- }
-
- };
- locationManager.requestLocationUpdates(provider,1000,10,locationListener);
-
- Geocodergeo=newGeocoder(context,Locale.getDefault());
- try
- {
- List<Address>address=geo.getFromLocation(location.getLatitude(),location.getLongitude(),1);
-
if(address.size()>0)
- {
- addres=address.get(0).getAddressLine(0);
- }
- }catch(IOExceptione)
- {
- e.printStackTrace();
- }
-
returnaddres;
- }
- }
分享到:
相关推荐
第一:当使用GPS定位时,最好不要使用getLastKnownLocation方法获得当前位置对象Location,因为该对 象可以在onLocationChanged的参数中由系统给予(根据文档,getLastKnownLocation有2方面功能:1. 获取当前地理...
方法获取Location对象** ``` String provider = LocationManager.GPS_PROVIDER;// 指定LocationManager的定位方法 Location location = locationManager.getLastKnownLocation(provider);// 调用...
可是一写进代码里,返回值(Location 类型)居然一直为null..郁闷的不得了。在网上查了好久,发现好多人都和我一样纠结于这个问题上,有人说是因为GPS没打开,也有人说是相关权限没加上..可是我的明明已经在设置里...
那么我来告诉大家,只有NETWORK_PROVIDER这种模式才是室内定位可靠的方式,就是当location为null的时候只要用这个,NETWORK_PROVIDER。 不过直接用大家也是用不了的,为啥呢,因为大部分厂商也不会用google的...
解决网络上常见的location返回为null问题,室内真机测试可能达不到结果,但是室外一定可以
在Android应用中实现GPS返回Location经纬度值 不为null 可以实现
Android锁屏无法继续定位问题 产生问题的原因: 手机锁屏后,Android系统为了省电以及减少CPU消耗,在一段时间后会将手机进入休眠状态。此时的服务以及线程等都会停止。 最近就这个问题,阅读了很多代码以及官方文档...
今天用Android自带的LocationManager和Location类实现定位,然而中间却出了点问题,那就是getBestProvider在真机上测试返回null值。查阅了很多方法都没有效果,于是我自己灵机一动想出了较好的解决办法。 基本代码...
if (location == null || mMapView == null) return; MyLocationData locData = new MyLocationData.Builder() .accuracy(location.getRadius()) //此处设置开发者获取到的方向信息,顺时针0-...
来获取location信息,但是往往在调试中loc是null的,因为首先需要在模拟器中手动添加GPS信息,有两种手动添加方法 1、在eclipse下,windows–>open perspective–>DDMS–>Emulator control–>Manual下手动设置经纬度...
定位城市或者详细信息百度地图act_person_location = (TextView) findViewById(R.id.act_person_location); mLocationClient = new LocationClient(context.getApplicationContext()); //声明LocationClient类 ...
//获取首次定位时间 String str; str="完成首次定位,耗时"+utc+"毫秒"; Toast.makeText(getApplicationContext(), str, Toast.LENGTH_LONG).show(); break; case ...
// 设置发起定位请求的间隔时间为5000ms // option.setIsNeedAddress(true); // 返回的定位结果包含地址信息 // option.setNeedDeviceDirect(true); // 返回的定位结果包含手机机头的方向 option.setAddrType(...
申请密钥Android定位SDK 每一个AndroidManifest.xml 中的package属性 对应一个AK,不可混用 iOS SDK开发密钥 每一个Bundle Identifier 对应一个AK,不可混用 二,安装插件```` cordova plugin add ...