首页 > 解决方案 > Google Maps API - 如何在不捕捉位置的情况下移动地图?

问题描述

作为一个学校项目,我开始自学使用 Android Studio,并希望在这个应用程序中使用 Google Maps API。

我有一张地图,上面有我的位置,但每次我移动相机时,它都会立即将我的相机重新定位到我的位置。

我目前的代码是这样的:

@Override
protected void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_maps);
    SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
            .findFragmentById(R.id.map);
    mapFragment.getMapAsync(this);
    locationManager = (LocationManager) getSystemService(LOCATION_SERVICE);
    BitmapDrawable bitmapDrawable = (BitmapDrawable) getResources().getDrawable(R.drawable.my_marker_icon);
    Bitmap b = bitmapDrawable.getBitmap();
    Bitmap smallMarker = Bitmap.createScaledBitmap(b, 40, 40, false);
    mo = new MarkerOptions().position(new LatLng(0, 0)).title("My Current Location").icon(BitmapDescriptorFactory.fromBitmap(smallMarker));
    if (Build.VERSION.SDK_INT >= 23 && !isPermissionGranted()) {
        requestPermissions(PERMISSIONS, PERMISSION_ALL);
    } else requestLocation();
    if (!isLocationEnabled())
        showAlert(1);
}

@Override
public void onMapReady(GoogleMap googleMap) {
    mMap = googleMap;
    marker = mMap.addMarker(mo);
}

@Override
public void onLocationChanged(Location location) {
    LatLng myCoordinates = new LatLng(location.getLatitude(), location.getLongitude());
    marker.setPosition(myCoordinates);
    CameraUpdate center = CameraUpdateFactory.newLatLng(myCoordinates);
    CameraUpdate zoom = CameraUpdateFactory.zoomTo(15.0f);
    mMap.moveCamera(center);
    mMap.animateCamera(zoom);

}

标签: androidgoogle-mapsmaps

解决方案


推荐阅读