1

I'm developing an Android app that is supposed to use Google Maps.

I used this code to track the change in zoom (from here link)

googleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
    @Override
    public void onCameraMove() {
        CameraPosition cameraPosition = googleMap.getCameraPosition();
        if(cameraPosition.zoom > 18.0) {
            googleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
        } else {
            googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
        }
    }
});

But in this case, the satellite map reloads at the slightest camera shift.

How to load an area of ​​a certain size so that the map does not load continuously?

1 Answers1

0

You need to change map type only if its current type is not what you need. Slightly modify your code to check map type before change it and change only if changes needed:

public class MainActivity extends AppCompatActivity implements OnMapReadyCallback {

    private GoogleMap mGoogleMap;

    ...
    @Override
    public void onMapReady(GoogleMap googleMap) {
        mGoogleMap = googleMap;

        mGoogleMap.setOnCameraMoveListener(new GoogleMap.OnCameraMoveListener() {
            @Override
            public void onCameraMove() {
                CameraPosition cameraPosition = mGoogleMap.getCameraPosition();
                if(cameraPosition.zoom > 18.0) {
                    if (mGoogleMap.getMapType() != GoogleMap.MAP_TYPE_HYBRID) {
                        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_HYBRID);
                    }
                } else {
                    if (mGoogleMap.getMapType() != GoogleMap.MAP_TYPE_NORMAL) {
                        mGoogleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
                    }
                }
            }
        });
    }
    ...

}
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79