我有一个包含mapView、缩放按钮和放大按钮的LinearLayout。 应用程序接收触摸事件,并根据给定的信息(X坐标,Y坐标,触摸事件类型)管理LinearLayout子项。 我能够使用以下代码进行缩放,但无法移动地图:
@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.getScreenRect()
、mapView.getBoundingBox()
、mapView.getLatitudeSpan()
等方法,并使用您的点或位置进行计算。 (为什么要删除已接受的答案,这仍然回答了您的问题...如果遇到问题,请考虑打开一个新的问题来了解如何进行此类计算)。 - BlacklightmapView.getZoomLevel()
获取当前缩放级别就足够了。请参阅 https://github.com/osmdroid/osmdroid/blob/master/osmdroid-android/src/main/java/org/osmdroid/views/MapView.java 以获取可用方法。 - Blacklight