Android地图 - 限制在180度

时间:2013-01-23 14:17:43

标签: android google-maps android-mapview

我可以将Google Maps V2(Android)限制在180°/ -180°经度(如iOS MapKit)吗?我不想让它环绕,因为我正在尝试实现一个clusterin算法,180 / -180度的分割会让它变得困难。

我希望在红线上限制平移:

enter image description here

2 个答案:

答案 0 :(得分:3)

所以我创建了一个应该没问题的解决方案。如果用户在-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;
    }
}

和xml:

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

对于GoogleMap(mapView.getMap()):

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

如果用户让地图“飞”到危险区域,则需要这样做。

答案 1 :(得分:1)

请参阅本教程了解v2http://econym.org.uk/gmap/range.htm - 基本上,您为移动事件添加了一个侦听器,如果移动超出范围,则中止移动。这也适用于v3