在我的地图上控制用户手势

control user gesture on my map

我是编码新手google地图, 我的问题是我如何控制用户手势拖动、放大和缩小。 因为当我 zoomin/out 时我的代码总是回到用户的当前位置,当我向上、向下、向左、向右拖动/滚动时。总是回到现在的位置。

这是我当前 loc 用户的代码

private GoogleMap.OnMyLocationChangeListener myLocationChangeListener = new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
            mMarker = mMap.addMarker(new MarkerOptions().position(loc));
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
        }
    };

您只能在第一次使用 boolean 移动相机:

public class MapsActivity extends FragmentActivity implements OnMapReadyCallback, GoogleMap.OnMyLocationChangeListener {
    private GoogleMap mMap;
    private Marker mMarker;
    private boolean firstTime = true;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        setContentView(R.layout.activity_maps);

        ((SupportMapFragment) getSupportFragmentManager()
                .findFragmentById(R.id.map)).getMapAsync(this);
    }

    @Override
    public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        mMap.setMyLocationEnabled(true);
        mMap.setOnMyLocationChangeListener(this);
    }

    @Override
    public void onMyLocationChange(Location location) {
        LatLng loc = new LatLng(location.getLatitude(), location.getLongitude());
        mMarker = mMap.addMarker(new MarkerOptions().position(loc));

        if (firstTime) {
            mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(loc, 16));
            firstTime = false;
        }
    }
}

注意:考虑到此示例使用 GoogleMap.OnMyLocationChangeListener 只是因为这是您在问题中使用的方法,但它已被弃用,您必须根据 the documentation 使用 FusedLocationProviderApi:

public final void setOnMyLocationChangeListener (GoogleMap.OnMyLocationChangeListener listener)

This method was deprecated. use com.google.android.gms.location.FusedLocationProviderApi instead. FusedLocationProviderApi provides improved location finding and power usage and is used by the "My Location" blue dot. See the MyLocationDemoActivity in the sample applications folder for example example code, or the Location Developer Guide.