我想找到我当前位置的经度和纬度,但我一直得到NulL.
double lat = loc.getLatitude(); //Cause the result is null, so can't kNow longitude and latitudedouble lng = loc.getLongitude(); LocationManager locationManager = (LocationManager) this.getSystemService(Context.LOCATION_SERVICE); String provIDer = LocationManager.GPS_PROVIDER; Location location = locationManager.getLastKNownLocation(provIDer); //result is null!
这是获取GPS状态的代码.它工作正常:
public voID onGpsstatusChanged(int event) { // get the GPS statue LocationManager locationManager = (LocationManager) GpsActivity.this.getSystemService(Context.LOCATION_SERVICE); Gpsstatus status = locationManager.getGpsstatus(null); String satelliteInfo = updateGpsstatus(event, status); myTextVIEw.setText(satelliteInfo);//work fine ,searched satellite:16 } }; private String updateGpsstatus(int event, Gpsstatus status) { StringBuilder sb2 = new StringBuilder(""); if (status == null) { sb2.append("searched satellite number" +0); } else if (event == Gpsstatus.GPS_EVENT_SATELliTE_STATUS) { int maxSatellites = status.getMaxSatellites(); Iterator<GpsSatellite> it = status.getSatellites().iterator(); numSatelliteList.clear(); int count = 0; while (it.hasNext() && count <= maxSatellites) { GpsSatellite s = it.next(); numSatelliteList.add(s); count++; } sb2.append("searched satellite number:" + numSatelliteList.size()); } return sb2.toString(); }
解决方法:
getLastKNownLocation()仅返回最近的GPS修复(如果可用).您需要实现LocationListener并使用LocationManager#requestLocationUpdates()
来获取新位置.
基本实施:
public class Example extends Activity implements LocationListener { LocationManager mLocationManager; @OverrIDe public voID onCreate(Bundle savedInstanceState) { super.onCreate(savedInstanceState); setContentVIEw(R.layout.main); mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE); Location location = mLocationManager.getLastKNownLocation(LocationManager.GPS_PROVIDER); if(location != null) { // Do something with the recent location fix // otherwise wait for the update below } else { mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this); } } @OverrIDe public voID onLocationChanged(Location location) { if (location != null) { Log.v("Location Changed", location.getLatitude() + " and " + location.getLongitude()); } } // etc..}
总结 以上是内存溢出为你收集整理的如何在Android中获得经度和纬度全部内容,希望文章能够帮你解决如何在Android中获得经度和纬度所遇到的程序开发问题。
如果觉得内存溢出网站内容还不错,欢迎将内存溢出网站推荐给程序员好友。
欢迎分享,转载请注明来源:内存溢出
评论列表(0条)