使用地图控制器和地理位置更新Google地图

时间:2012-07-26 04:25:35

标签: android google-maps

我在这里。我在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和manifest中没有问题,因为Google Map本身正在显示。谢谢你的帮助。

1 个答案:

答案 0 :(得分:0)

你还记得转换成微度的吗?从文档中:构造一个具有给定纬度和经度的GeoPoint,以微度(度* 1E6)为单位。因此,您需要将位置监听器的纬度和长度乘以1E6,否则它将最有可能生成非洲海岸附近的位置。