Search code examples
androidandroid-mapviewosmdroidandroid-framelayouttouch-event

panning not working correctly in mapView


I have mapView and I programatically handle the touch events.

The problem is: if the mapview is ZoomOut to its maximum, the panning is quite small and slow, and conforming the mapView is ZoomIn, the panning is bigger and faster. In another words: bigger the zoomIN bigger the panning movements.

How can I make panning moves not accordingly to zoom , so every time I move the map, it moves the same distance no matter the zoom level?

Here is my android Client

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();

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

....

//move mapView
private boolean doPanning(MotionEvent e, MapView mapView)
{
if(savedTouchedX >= 0 && savedTouchedY >= 0)
{
    IGeoPoint mapCenter = mapView.getMapCenter();
    GeoPoint panToCenter = new GeoPoint((int)(mapCenter.getLatitudeE6() + (e.getY() - savedTouchedY) * 1E5),
    (int)(mapCenter.getLongitudeE6() - (e.getX() - savedTouchedX) * 1E5));
    mapView.getController().setCenter(panToCenter);
}
savedTouchedX = e.getX();
savedTouchedY = e.getY();
return true;
}

Solution

  • Based on TouchEvent, move the map programmatically

    yourFrameLayout.setOnTouchListener(new OnTouchListener() {
    
        @Override
        public boolean onTouch(View v , MotionEvent event) {
            return gestureDetector.onTouchEvent(event);
        }
    });
    
    SimpleOnGestureListener listener = new SimpleOnGestureListener() {
        @Override
        public boolean onFling(MotionEvent e1 , MotionEvent e2 , float velocityX , float velocityY) {
    
            float Xdirection = e1.getX() - e2.getX();
            float Ydirection = e1.getY() - e2.getY();
    
            //TODO- Do some calculations & change the maps lat & lon. 
            // Also consider zoom level while calculating.
    
            final double newLat = (getOldLat - (moveSomeLatBasedOnXDirection)));
            final double newLon = (getOldLon - (moveSomeLatBasedOnYDirection)));
    
            CameraUpdate center = CameraUpdateFactory.newLatLng(new LatLng( newLat, newLon));
    
            map.moveCamera(center);
    
    
            return super.onFling(e1, e2, velocityX, velocityY);
        };
    
    };
    GestureDetector gestureDetector = new GestureDetector(getActivity(), listener);