Android getLastKnownLocation返回NULL
我刚刚让它起作用,但当我关闭Wifi以查看是否可以使用GPS获得更准确的位置时,它现在给我带来了一个Android getLastKnownLocation返回NULL,android,gps,Android,Gps,我刚刚让它起作用,但当我关闭Wifi以查看是否可以使用GPS获得更准确的位置时,它现在给我带来了一个NullPointer错误,我不知道为什么 下面的代码首先被调用 public void onConnected(Bundle dataBundle) { connected = true; //Request an update from the location client to get current Location details mL
NullPointer
错误,我不知道为什么
下面的代码首先被调用
public void onConnected(Bundle dataBundle) {
connected = true;
//Request an update from the location client to get current Location details
mLocationClient.requestLocationUpdates(mLocationRequest,this);
Toast.makeText(this, "Connected", Toast.LENGTH_SHORT).show();
}
这就需要调用这个方法
public void onLocationChanged(android.location.Location location) {
// Report to the UI that the location was updated
String msg = "Updated Location: " +
Double.toString(location.getLatitude()) + "," +
Double.toString(location.getLongitude());
if(tmp != location.getLatitude())
{
Toast.makeText(this, msg, Toast.LENGTH_SHORT).show();
}
tmp = location.getLatitude();
}
你可以看到我在那里有两个祝酒词,它们与实际的GPS坐标一致,但是当我调用下面的代码时,它会在新的定位线上崩溃getLastKnownLocation
public String getLocation()
{
// Get the location manager
LocationManager locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
Criteria criteria = new Criteria();
String bestProvider = locationManager.getBestProvider(criteria, false);
android.location.Location location = locationManager.getLastKnownLocation(bestProvider);
Double lat,lon;
DateFormat dateFormat = new SimpleDateFormat("yyyy/MM/dd HH:mm:ss");
Date date = new Date();
try {
lat = location.getLatitude ();
lon = location.getLongitude ();
Toast.makeText(this,""+lat.toString()+"-"+lon.toString(),Toast.LENGTH_SHORT).show();
return new LatLng(lat, lon).toString();
}
catch (NullPointerException e){
Toast.makeText(this,"HELL-NO",Toast.LENGTH_SHORT).show();
Log.e("HELL-NO","n",e);
e.printStackTrace();
return null;
}
}
我只是不明白为什么当我试图检索位置时,它似乎是在
onLocationChanged
方法中检索到的,它只会出现一个空指针。当你不应该的时候,你正在将新的和旧的位置API混合在一起
要获得最后一个已知位置,您只需打电话
mLocationClient.getLastLocation();
连接位置服务后
阅读如何使用新的位置API
我只是改变了
locationManager.getLastKnownLocation(LocationManager.GPS_PROVIDER) to
locationManager.getLastKnownLocation(LocationManager.PASSIVE_PROVIDER);
这为我解决了问题。完整代码段:
map = ((MapFragment) getFragmentManager().
findFragmentById(R.id.map)).getMap();
map.setMyLocationEnabled(true);
locationManager = (LocationManager)getSystemService
(Context.LOCATION_SERVICE);
getLastLocation = locationManager.getLastKnownLocation
(LocationManager.PASSIVE_PROVIDER);
currentLongitude = getLastLocation.getLongitude();
currentLatitude = getLastLocation.getLatitude();
currentLocation = new LatLng(currentLatitude, currentLongitude);
希望这有帮助 这件事我挣扎了很长时间。但谷歌刚刚提出了一个解决方案
googleMap.getMyLocation();
要获取api V2上蓝点的位置,请尝试以下代码:
LocationManager mLocationManager;
Location myLocation = getLastKnownLocation();
private Location getLastKnownLocation() {
mLocationManager = (LocationManager)getApplicationContext().getSystemService(LOCATION_SERVICE);
List<String> providers = mLocationManager.getProviders(true);
Location bestLocation = null;
for (String provider : providers) {
Location l = mLocationManager.getLastKnownLocation(provider);
if (l == null) {
continue;
}
if (bestLocation == null || l.getAccuracy() < bestLocation.getAccuracy()) {
// Found best last known location: %s", l);
bestLocation = l;
}
}
return bestLocation;
}
LocationManager mLocationManager;
位置myLocation=getLastKnownLocation();
私有位置getLastKnownLocation(){
mlLocationManager=(LocationManager)getApplicationContext().getSystemService(位置服务);
List providers=mLocationManager.getProviders(true);
位置bestLocation=null;
for(字符串提供程序:提供程序){
位置l=mLocationManager.getLastKnownLocation(提供者);
if(l==null){
继续;
}
if(bestLocation==null | | l.getAccuracy()
Cheers@tyczj,不知道有“新“API,再次感谢!此api返回设备中已存储的位置?或者当我们调用它时返回实时位置?它对我有用,但是GPS_提供商和被动_提供商之间有什么区别?@TroyZuroske知道被动提供商为什么解决这个问题吗?抱歉撞到你了。也为我工作,谢谢!它不起作用。。。。Location Location=locationManager.getLastKnownLocation(locationManager.PASSIVE\u提供程序);Log.d(标记“OnRes:lastnown:”+locationManager.getlastnownlocation(locationManager.PASSIVE_PROVIDER));Log.d(标签“OnRes:lastknown:”+位置);在Logcate中给我null您正在混合getLastLocation的新api和旧api