0

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!

damian
  • 2,001
  • 20
  • 38
  • It doesn't work if you `setClickable(false)` and add the zoom controls? – jnthnjns Aug 29 '12 at 21:32
  • Post your code. You've to disable **postTranslate** for that – Numair Aug 29 '12 at 21:32
  • My code won't bring us any further, because it's simply a MapView in the fragment. If I disable clickable and add zoom controls, how will it behave if someone has hardware buttons and focusses the map, then uses they keyboard or something to move? besides, I'd really like to keep pinch to zoom, just with a fixed centre.. zoom controls would be the last resort, but pinch to zoom is way better I think – damian Aug 29 '12 at 21:47
  • I've extended the MapView class and override computeScroll() method, as described here http://stackoverflow.com/a/14247266/1005652 – comeGetSome Jan 09 '13 at 22:02

1 Answers1

2

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;
  }
 } 
 });
A Random Android Dev
  • 2,691
  • 1
  • 18
  • 17
  • Could you give me an example of how you would do this? I imagine, that pinch to zoom would not work correct if I overwrite dispatchDraw and only "call super" if it's the same center, as when pinching you often "drag" it as well, so I'd need to overwrite the touch stuff to "reset the center to my center" everytime when there are 2 pointers, or am I thinking too complicated? – damian Aug 29 '12 at 22:17
  • Please check the edit I've made to my answer, this code works perfectly! – A Random Android Dev Aug 30 '12 at 00:54