MapView - Disable dragging around, but allow zooming

白昼怎懂夜的黑 提交于 2019-12-12 05:32:17

问题


I have a ViewPager with 3 fragments inside, on the outer right I want to display a MapView - working fine so far. But I'd like to disable dragging it around, but still let the user zoom in / out - with a fixed center. If the user would be allowed to drag the map around, it messes with the viewpager scrolling abilities. Tried to disable clickable + focusable but - of course - no zooming anymore..

Looked at other questions here at stackoverflow but none provided a working solution so far..

Thanks!


回答1:


Ignore everything I've said so far (which is why I've edited my answer), instead use the following code in your MapActivity class(it works perfectly, just tried it!):

final GeoPoint point = new GeoPoint(latitudeE6, longitudeE6);
final MapController mapController = mapView.getController();

mapController.animateTo(point);
mapController.setZoom(6);

mapView.setOnTouchListener(new View.OnTouchListener() {

@Override
public boolean onTouch(View arg0, MotionEvent arg1) {
  if(arg1.getAction() ==  MotionEvent.ACTION_UP)
  {
    mapController.setCenter(point);
    return true;
  }
  if(arg1.getPointerCount() > 1)
  {
   mapController.setCenter(point);
   return false;
  }
  else
  {
   return true;
  }
 } 
 });


来源:https://stackoverflow.com/questions/12186801/mapview-disable-dragging-around-but-allow-zooming

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!