无法移动相机跟随Google地图中的标记

问题描述 投票:0回答:1

我有一个奇怪的问题。我正在编写一个使用Google地图框架的应用程序,由于某种奇怪的原因,标记可以完美移动,但是相机不能跟随它。这是我的onLocationChanged方法。

@Override
    public void onLocationChanged(Location location) {

        if (currentMarker != null) {
            currentMarker.remove();
        }

        LatLng latLng = new LatLng(location.getLatitude(),
                location.getLongitude());

        MarkerOptions options = new MarkerOptions();
        options.position(latLng);
        options.icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_MAGENTA));

        currentMarker = mMap.addMarker(options);
        CameraUpdate center=
                CameraUpdateFactory.newLatLng(latLng);
        CameraUpdate zoom=CameraUpdateFactory.zoomTo(15.0f);

        mMap.moveCamera(center);
        mMap.animateCamera(zoom);

        if(client != null) {
            LocationServices.FusedLocationApi.removeLocationUpdates(client, this);
        }
    }

再次,我一生无法理解问题所在

android google-maps location on-location-changed
1个回答
0
投票

zoom CameraUpdate包含您实际移动相机的位置之前,因此mMap.animateCamera(zoom)只是将其从前一个moveCamera移回。要解决,请尝试:

CameraUpdate center= CameraUpdateFactory.newLatLngZoom(latLng,15.0f);
mMap.animateCamera(center);

CameraUpdate center = CameraUpdateFactory.newLatLng(latLng);
mMap.moveCamera(center);
CameraUpdate zoom= CameraUpdateFactory.zoomTo(15.0f);
mMap.animateCamera(zoom);
© www.soinside.com 2019 - 2024. All rights reserved.