覆盖osmdroid MapView的默认平移/移动

时间:2014-07-17 20:31:51

标签: android android-mapview osmdroid touch-event panning

我有mapView,我以编程方式处理触摸事件。

问题是:如果mapview的ZoomOut达到最大值,则平移非常小且速度慢,并且使mapView符合ZoomIn,平移越来越大。换句话说:zoomIN越大,平移动作越大。

如何进行平移移动而不是相应的缩放,所以每次移动地图时,无论缩放级别如何,都会移动相同的距离? 或者是否有另一种方法来覆盖mapview中的平移?

这是我的Android客户端

private double savedTouchedX = -1;
private double savedTouchedY = -1;
....

mapView.setOnTouchListener(new MapView.OnTouchListener() {

        @Override
        public boolean onTouch(View arg0, MotionEvent event) {
            int action = event.getAction();
            final Projection pj = mapView.getProjection();
            GeoPoint eventPosition = (GeoPoint) pj.fromPixels((int)event.getX(), (int)event.getY());

             switch(action)
             {
                case MotionEvent.ACTION_DOWN:
                    savedTouchedX = eventPosition.getLongitude();
                    savedTouchedY = eventPosition.getLatitude();
                    break;
                case MotionEvent.ACTION_MOVE:
                    doPanning(eventPosition, mapView);
                    break;
                case MotionEvent.ACTION_UP:
                    doPanning(eventPosition, mapView);
                    savedTouchedX = -1;
                    savedTouchedY = -1;
                    break;
                default:
                    break;
             }
             return true;
        }
    });

//move mapView
private boolean doPanning(GeoPoint e, MapView mapView)
 {
        GeoPoint mapCenter = (GeoPoint) mapView.getMapCenter();
        GeoPoint panToCenter = new GeoPoint((int)(mapCenter.getLatitudeE6() + (e.getLatitude() - savedTouchedY) * 1E5),
        (int)(mapCenter.getLongitudeE6() - (e.getLongitude() - savedTouchedX) * 1E5));
        mapView.getController().setCenter(panToCenter);
    savedTouchedX = e.getLongitude();
    savedTouchedY = e.getLatitude();
    return true;
 }

1 个答案:

答案 0 :(得分:1)

您必须在Map位置转换MotionEvent位置(在屏幕坐标中)。 这根本不是相同的坐标系!

final Projection pj = mapView.getProjection();
GeoPoint eventPosition = (GeoPoint) pj.fromPixels((int)event.getX(), (int)event.getY());