我只需要一次在 GPS 中的实际位置。在这段代码中,永远不会调用 onLocationChanged,我不明白为什么。正如我所说:
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
它应该每 0 秒和每 0 米运行一次。如何更改代码以使其正常工作?
public class MapActivity extends FragmentActivity implements
LocationListener {
private final String TAG = getClass().getSimpleName();
private GoogleMap mMap;
private Location loc;
private Context ctx;
private radius;
LocationManager mLocationManager;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);
mLocationManager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
mLocationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, 0, 0, this);
ctx = MapActivity.this;
radius = getRadius();
mMap = initMap();
}
@Override
public void onLocationChanged(Location location) {
// TODO Auto-generated method stub
if (location != null) {
loc = location;
Log.v("Location Changed", location.getLatitude() + " and "
+ location.getLongitude());
mLocationManager.removeUpdates(this);
myPlaces = new GetPlaces(ctx, GAS_STATION, mMap, loc, radius);
myPlaces.execute();
Log.e(TAG, "location : " + loc);
}
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
编辑:我将呼叫移至 loc :
myPlaces = new GetPlaces(ctx, GAS_STATION, mMap, loc, radius);
myPlaces.execute();
Log.e(TAG, "location : " + loc);
在 onLocationChange 内部。这样,我不再得到 nullpointerException,但我可以等待超过 20 秒才能获得位置。这不太好......如果有人知道如何快速修复!