Dany19
Dany19

Reputation: 551

how to programmatically move a mapView (osmdroid)

I have a LinearLayout with a mapView, zoomIn button, zoomOut button. the app receives touchevents and it is supposed to manage the LinearLayout children with the given information (X coordinate, Y coordinate , type of touch event) I am able to zoomIn and ZoomOut with the following code, but I am not able to move the map:

@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;
}
....
public void lookForButton(String msg){
    String[] strArray = msg.split(" ");
    final MotionEvent m;
    int x=Integer.valueOf(strArray[1]);
    int y=Integer.valueOf(strArray[2]);
    int type=Integer.valueOf(strArray[3]);
    switch (type){
        case 2:
            m = MotionEvent.obtain(226707,226707,0/*ACTION_DOWN*/,x,y,0);
            runOnUiThread(new Runnable() {
                public void run() {
                    memecontentView.dispatchTouchEvent(m);
                }
            });
            break;
    case 3:
        m = MotionEvent.obtain(226707,226707,1/*ACTION_UP*/,x,y,0);
        runOnUiThread(new Runnable() {
            public void run() {
                memecontentView.dispatchTouchEvent(m);
            }
        }); 
        break;
    case 5:
        m = MotionEvent.obtain(226707,226707,2/*ACTION_MOVE*/,x,y,0);
        runOnUiThread(new Runnable() {
            public void run() {
                memecontentView.dispatchTouchEvent(m);

            }
        }); 
        break;  
    }

}
public void ZoomInButton(View v){
    mapView.getController().zoomIn();
}
public void ZoomOutButton(View v){
    mapView.getController().zoomOut();
}

How can I programmatically move mapview with the given information (X coordinate, Y coordinate , type of touch event)?

Upvotes: 3

Views: 3361

Answers (1)

Blacklight
Blacklight

Reputation: 3827

You can call mapView.scrollTo(x, y).

Another possibility is mapView.getController().animateTo() (with animation) or mapView.getController().setCenter() (without animation).

It takes an IGeoPoint as a parameter which you can instantiate with your coordinates:

new GeoPoint(latitude, longitude)

Upvotes: 7

Related Questions