编程语言
首页 > 编程语言> > java – 如何以编程方式移动mapView(osmdroid)

java – 如何以编程方式移动mapView(osmdroid)

作者:互联网

我有一个带有mapView,zoomIn按钮,zoomOut按钮的LinearLayout.
应用程序接收touchevents,它应该使用给定的信息(X坐标,Y坐标,触摸事件的类型)管理LinearLayout子项
我可以使用以下代码zoomIn和ZoomOut,但我无法移动地图:

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

如何以编程方式使用给定信息(X坐标,Y坐标,触摸事件类型)移动mapview?

解决方法:

您可以调用mapView.scrollTo(x,y).

另一种可能性是mapView.getController().animateTo()(带动画)或mapView.getController().setCenter()(没有动画).

它将IGeoPoint作为参数,您可以使用坐标进行实例化:

new GeoPoint(latitude, longitude)

标签:android,java,android-mapview,osmdroid,touch-event
来源: https://codeday.me/bug/20190830/1769649.html