Android Get Mapview on Fling stopped animating

醉酒当歌 提交于 2020-01-14 14:52:48

问题


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 onTouchEvent. I am detecting the action "ACTION_UP" and checking the coords here and repostioning the map if I have to. Everything works fine until the user "flings" the map. I can detect the "Up" and the coordinates of the screen at that point, but the map is still moving so the coordinates I detect aren't the correct ones.

I need to know the screen position when it's stopped moving!

    @Override
    public boolean onTouchEvent(MotionEvent ev) {
        if (ev.getAction() == MotionEvent.ACTION_UP ) {
//get coords of screen corners and calculate if the map is still in view.
}

I have been looking for a fair amount of time for the answer to this but lots of people are asking the question, but there don't appear to be any solutions around?

Has anyone managed to do this?

Bex


回答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...




回答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;
}


来源:https://stackoverflow.com/questions/5950429/android-get-mapview-on-fling-stopped-animating

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