即使我点击了标记,地图仍会返回我的位置

Map keeps coming back to my location even after i clicked on a marker

假设我在位置 x。我的地图完美加载并缩放到我的位置。现在我点击我在另一个位置创建的标记,比方说位置 y。相机改变并转到位置 y 的那个标记,因为它应该。 问题是,它在那里停留大约 2-3 秒,然后回到我的位置(位置 x)。 我怎样才能让它留在我想要的地方,直到我把它改回来? 这是我认为需要更改的部分代码。 如果您需要更多信息、更多代码,请随时询问。

public void setUpMap() {
    map.setMyLocationEnabled(true);
    map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
        @Override
        public void onMyLocationChange(Location location) {
            LatLng l = new LatLng(location.getLatitude(), location.getLongitude());
            map.animateCamera(CameraUpdateFactory.newLatLngZoom(l, 12));

        }
    });

每次地图收到位置更新时,您的相机都会更新。为避免这种情况,只需删除 OnMyLocationChangeListener.

我知道您只需要在地图首次加载时以您的位置为中心。在这种情况下,您可以在第一次使用 map.setOnMyLocationChangeListener(null);:

调用时从地图中删除 OnMyLocationChangeListener
map.setMyLocationEnabled(true);
map.setOnMyLocationChangeListener(new GoogleMap.OnMyLocationChangeListener() {
    @Override
    public void onMyLocationChange(Location location) {
        LatLng l = new LatLng(location.getLatitude(), location.getLongitude());
        map.animateCamera(CameraUpdateFactory.newLatLngZoom(l, 12));

        map.setOnMyLocationChangeListener(null); // Remove the listener
    }
});

此外,请考虑到,根据 the documentation of GoogleMap.OnMyLocationChangeListener:

This interface is 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.