我又来了。我有另一个关于 android 中的谷歌地图的问题。我正在尝试更新地图视图中显示的地图,这样当用户的位置通过 gps 更新时,地图将显示该位置的地图。
我目前正在使用 GeoPoint 和 MapController 但我没有得到结果。相反,它动画到错误的位置。我尝试交换经度和纬度值,但结果仍然相同。
这是我的活动的源代码。
public class MyActivity extends MapActivity {
LocationManager manager;
Location currentLocation;
MapController mapController;
TextView locationView;
MapView mapView;
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.main);
locationView = (TextView) findViewById(R.id.textView);
mapView = (MapView) findViewById(R.id.mapview);
mapView.setBuiltInZoomControls(true);
mapController = mapView.getController();
mapController.setZoom(5);
manager = (LocationManager) getSystemService(Context.LOCATION_SERVICE);
}
@Override
protected void onPause() {
super.onPause();
manager.removeUpdates(listener);
}
@Override
protected void onResume() {
super.onResume();
if (!manager.isProviderEnabled(LocationManager.GPS_PROVIDER)) {
AlertDialog.Builder builder = new AlertDialog.Builder(this);
builder.setTitle("Location Manager");
builder.setMessage("We want to use your location, but GPS is currently disabled.\nWould you like to change these settings now?");
builder.setPositiveButton("Yes",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
Intent i = new Intent(
Settings.ACTION_LOCATION_SOURCE_SETTINGS);
startActivity(i);
}
});
builder.setNegativeButton("No",
new DialogInterface.OnClickListener() {
@Override
public void onClick(DialogInterface dialog, int which) {
finish();
}
});
builder.create().show();
}
currentLocation = manager
.getLastKnownLocation(LocationManager.GPS_PROVIDER);
updateDisplay();
int minTime = 5000;
float minDistance = 0;
manager.requestLocationUpdates(LocationManager.GPS_PROVIDER, minTime,
minDistance, listener);
}
private void updateDisplay() {
if (currentLocation == null) {
locationView.setText("Determining your location...");
} else {
locationView.setText(String.format("Your location:\n%.2f, %.2f",
currentLocation.getLatitude(),
currentLocation.getLongitude()));
}
}
private LocationListener listener = new LocationListener() {
@Override
public void onStatusChanged(String provider, int status, Bundle extras) {
}
@Override
public void onProviderEnabled(String provider) {
}
@Override
public void onProviderDisabled(String provider) {
}
@Override
public void onLocationChanged(Location location) {
currentLocation = location;
GeoPoint point = new GeoPoint((int) currentLocation.getLatitude(),
(int) currentLocation.getLongitude());
mapController.setCenter(point);
updateDisplay();
}
};
@Override
protected boolean isRouteDisplayed() {
return false;
}
}
我很确定 xml 和清单中没有问题,因为谷歌地图本身正在显示。谢谢您的帮助。