我刚刚开始工作但是当我关闭我的Wifi以确定我是否可以使用我的GPS获得更准确的位置时,它现在给我带来了NullPointer
错误,我看不出原因。< / p>
首先调用以下代码;
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
方法中检索到了,它只是出现了一个nullpointer。
答案 0 :(得分:29)
我只是改变了
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);
希望这会有所帮助。
答案 1 :(得分:7)
当你不应该将新的和旧的位置API混合在一起时。
要获取最后一个已知位置,您只需致电
mLocationClient.getLastLocation();
连接位置服务后。
阅读如何使用新的位置API
http://developer.android.com/training/location/retrieve-current.html#GetLocation
答案 2 :(得分:0)
我很长时间都在努力解决这个问题。但谷歌刚刚想出了一个解决方案。
googleMap.getMyLocation();
获取api V2上蓝点的位置。
答案 3 :(得分:0)
试试这段代码:
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;
}