I need to limit 开发者_如何学JAVAthe 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
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...
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;
}
精彩评论