animateCamera works and moveCamera doesn't for GoogleMap - Android

后端 未结 4 1492
感情败类
感情败类 2021-01-14 16:36

I need to move Camera to cover all Markers on it. So, I build LatLngBounds and then try to call mMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLng

4条回答
  •  深忆病人
    2021-01-14 16:59

    Since Google Maps SDK was updated to recent versions onCameraChangeListener became deprecated. I've also faced with this problem and I discovered that onCameraIdleListener does the similar trick. As I see so far it's callback method onCameraIdle called always after onMapReady. So my approach looks like this piece of code (considering it put in Activity) for Google Maps SDK 9.6+:

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        // set content view and call getMapAsync() on MapFragment
    }
    
    @Override
    public void onMapReady(GoogleMap googleMap) {
        map = googleMap;
        map.setOnCameraIdleListener(this);
        // other initialization stuff
    }
    
    @Override
    public void onCameraIdle() {
        /* 
           Here camera is ready and you can operate with it. 
           you can use 2 approaches here:
    
          1. Update the map with data you need to display and then set
             map.setOnCameraIdleListener(null) to ensure that further events
             will not call unnecessary callback again.
    
          2. Use local boolean variable which indicates that content on map
             should be updated
        */
    }
    

提交回复
热议问题