简体   繁体   English

Android:Google Maps Camera动画

[英]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. 最初,此标志将设置为true。 If true, you would animate your Google map. 如果为true,则将为Google地图设置动画。 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"));
    }

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

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