mapview Java,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

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值