我想找到我当前位置的经度和纬度,但我一直得到NULL。
double lat = loc.getLatitude(); //Cause the result is null, so can't know longitude and latitude
double 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();
}