Reputation: 61
In my app I use GoogleMap (play-services-maps:10.2.1). I've fixed the position of the map on a specific location and I don't want my user to be able move the map. I only want him to be able to zoom on it.
Here's what I tried :
// Set position
LatLng requestedPosition = new LatLng(lat, lon);
map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
// Disable all Ui interaction except for zoom
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(true);
It looks like it work at first sight but in fact while zooming and dezooming the camera position change a little on every zoom movement.
I have no idea what to do.
Thanks for your help
Upvotes: 2
Views: 3548
Reputation: 32178
If I understand correctly, you would like to preserve a centre position after the zoom gestures. Zooming with the gesture doesn't maintain the same centre, you should correct position of camera once the gesture zoom is over. You can listen the idle event after zooming and animate the camera to the initial centre position.
Code snippet
public class MapsActivity extends FragmentActivity implements OnMapReadyCallback,
GoogleMap.OnCameraIdleListener {
private GoogleMap map;
private LatLng requestedPosition;
@Override
protected void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_maps);
// Obtain the SupportMapFragment and get notified when the map is ready to be used.
SupportMapFragment mapFragment = (SupportMapFragment) getSupportFragmentManager()
.findFragmentById(R.id.map);
mapFragment.getMapAsync(this);
}
@Override
public void onMapReady(GoogleMap googleMap) {
map = googleMap;
// Add a marker in Sydney and move the camera
requestedPosition = new LatLng(41.385692,2.163953);
float zoom = 16.0f;
map.addMarker(new MarkerOptions().position(requestedPosition).title("Marker in Barcelona"));
map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
//map.moveCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
// Disable all Ui interaction except for zoom
map.getUiSettings().setAllGesturesEnabled(false);
map.getUiSettings().setZoomGesturesEnabled(true);
map.setOnCameraIdleListener(this);
}
@Override
public void onCameraIdle() {
float zoom = map.getCameraPosition().zoom;
map.animateCamera(CameraUpdateFactory.newLatLngZoom(requestedPosition, zoom));
}
}
I placed this sample at Github https://github.com/xomena-so/so43733628
Hope this helps!
Upvotes: 2