Android Maps - 180 degree limit

Can I limit Google Maps V2 (Android) to 180 ° / -180 ° longitude (e.g. iOS MapKit)? I don’t want it to wrap itself around as I am trying to implement a cluster algorithm and the 180 / -180 split will be difficult.

I would like the panning to be limited to the red line:

enter image description here

+6
source share
2 answers

So, I created a solution that should be fine. If the user places a card on the -180/180 border, the card will flip over to the other side. So packaging the card is still possible, but the “dangerous” area is never displayed.

I had to create a custom MapView:

public class CustomMapView extends MapView { private double prevLongitude; @Override public boolean onInterceptTouchEvent(MotionEvent ev) { boolean retVal = correctCamera(); prevLongitude = getMap().getCameraPosition().target.longitude; return retVal; } public boolean correctCamera() { if (getMap().getProjection().getVisibleRegion().latLngBounds.northeast.longitude < getMap().getProjection().getVisibleRegion().latLngBounds.southwest.longitude) { double diff = getMap().getProjection().getVisibleRegion().latLngBounds.southwest.longitude - getMap().getProjection().getVisibleRegion().latLngBounds.northeast.longitude; double longitudeSW; double longitudeNE; double longitudeDiff = (360-diff) / 25; // use > 0 if you want the map to jump to the other side // <= 0 will cause the map to flip back if (prevLongitude > 0) { longitudeSW = -180 + longitudeDiff; longitudeNE = -180 + longitudeDiff - diff; } else { longitudeSW = 180 - longitudeDiff + diff; longitudeNE = 180 - longitudeDiff; } LatLngBounds bounds = new LatLngBounds( new LatLng(getMap().getCameraPosition().target.latitude, longitudeSW), new LatLng(getMap().getCameraPosition().target.latitude, longitudeNE) ); getMap().animateCamera(CameraUpdateFactory.newLatLngBounds(bounds, 0)); return true; } return false; } } 

And xml:

 <com.ieffects.clustermap.CustomMapView android:id="@+id/map" android:layout_width="match_parent" android:layout_height="match_parent" /> 

And for GoogleMap (mapView.getMap ()):

 map.setOnCameraChangeListener(new OnCameraChangeListener() { @Override public void onCameraChange(CameraPosition position) { mapView.correctCamera(); } }); 

This is necessary if the user allowed the card to "fly" to a dangerous area.

+3
source

See this tutorial for v2 : http://econym.org.uk/gmap/range.htm - basically you add a listener for the move event and abort the move if it is out of range. This should be applicable in v3 .

+1
source

All Articles