D-3*_*-32 6 android google-maps android-mapview
我可以将Google Maps V2(Android)限制在180°/ -180°经度(如iOS MapKit)吗?我不想让它环绕,因为我正在尝试实现一个clusterin算法,而180/-180度的分割会让它变得困难.
我希望在红线上限制平移:

所以我创建了一个应该没问题的解决方案。如果用户将地图平移到 -180 / 180 边界,地图将翻转到另一侧。因此,包裹地图仍然是可能的,但“危险”区域永远不会显示。
我必须创建一个自定义 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;
}
}
Run Code Online (Sandbox Code Playgroud)
和 XML:
<com.ieffects.clustermap.CustomMapView
android:id="@+id/map"
android:layout_width="match_parent"
android:layout_height="match_parent" />
Run Code Online (Sandbox Code Playgroud)
对于 GoogleMap (mapView.getMap()):
map.setOnCameraChangeListener(new OnCameraChangeListener() {
@Override
public void onCameraChange(CameraPosition position) {
mapView.correctCamera();
}
});
Run Code Online (Sandbox Code Playgroud)
如果用户让地图“飞”到危险区域,则需要这样做。