简体   繁体   中英

Android: Google Maps Camera animation

如何在不故意使用onLocationChanged的情况下将摄像机动画化(移动)到当前位置(蓝点)。

This is really on the fence between a comment and an answer, but I think you can get the behavior you want by just using an activity level boolean flag. Initially, this flag would be set to true. If true, you would animate your Google map. Otherwise, you would not animate. Something like this:

public class MapsActivity extends FragmentActivity
    implements OnMapReadyCallback, LocationListener {

    private GoogleMap googleMap;
    private boolean firstRender = true;

    @Override
    public void onLocationChanged(Location location) {
        CameraUpdate current = CameraUpdateFactory.newLatLngZoom(coordinates,15);
        if (firstRender) {
            googleMap.animateCamera(current);
            firstRender = false;
        }
        else {
            googleMap.moveCamera(current);
        }
    }
}

只需将您的相机而不是animateCamera移到MapReady上即可:mGoogleMap.moveCamera(CameraUpdateFactory.newCameraPosition(您的位置))

public void onMapReady(GoogleMap googleMap) {
        mMap = googleMap;
        // Add a marker in Sydney, Australia, and move the camera.
        LatLng chennai = new LatLng(new GPSTracker(activity).getLatitude(), new GPSTracker(activity).getLongitude());
        mMap.animateCamera(CameraUpdateFactory.newLatLngZoom(chennai, 12));
        mMap.addMarker(new MarkerOptions().position(chennai).title("Marker in chennai"));
    }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM