Skip to content Skip to sidebar Skip to footer

Android Get Mapview On Fling Stopped Animating

I need to limit the area my users can navigate to in the mapview (unless they'll get a blank screen!). I have created a class that extends the mapview and overridden the onTouchEve

Solution 1:

i use to override the computeScroll() method of the MapView:

/**
 * checks restriction on pan bounds and locks the map. if the map is locked
 * (via mapController.getCenter()) this method returns true
 * 
 * @return TRUE if map has been locked
 */
private boolean restrictPanOnBounds() {
  if (this.minLatitudeE6 == Integer.MIN_VALUE)
    return false;

  GeoPoint center = getMapCenter();
  int cLat = center.getLatitudeE6();
  int cLong = center.getLongitudeE6();

  Integer nLat = null, nLong = null;

  if (cLat < this.minLatitudeE6)
    nLat = this.minLatitudeE6;
  else if (cLat > this.maxLatitudeE6)
    nLat = this.maxLatitudeE6;

  if (cLong < this.minLongitudeE6)
    nLong = this.minLongitudeE6;
  else if (cLong > this.maxLongitudeE6)
    nLong = this.maxLongitudeE6;

  if (nLat != null || nLong != null) {
    getController().setCenter(new GeoPoint(nLat != null ? nLat : cLat, nLong != null ? nLong : cLong));
    return true;
  } else {
    return false;
  }
}

@Override
public void computeScroll() {
  if (restrictPanOnBounds())
    getController().stopPanning();
  else
    super.computeScroll();
}

it works quite well for simple move actions ( the map stops has does not jump back ) but still has a "funny" tilt effect when flinging...


Solution 2:

I had the same problem. Your approach is good, just need to catch the event on another place. You can override "DispatchTouchEvent" method of MapView class (or take similar approach using MapFragment), and there you make it skip MotionEvent.ACTION_UP event:

if (ev.getAction() == MotionEvent.ACTION_UP ) {
   return false;
}

Post a Comment for "Android Get Mapview On Fling Stopped Animating"