我有一个使用谷歌地图的应用程序。我在地图上也有叠加层。
public class OverLayClass extends ItemizedOverlay<OverlayItem>
{
private Drawable marker = null;
private OverlayItem inDrag = null;
private ImageView dragImage =null;
private int xDragImageOffset, yDragImageOffset, xDragTouchOffset, yDragTouchOffset;
public OverLayClass(Drawable marker)
{
super(boundCenterBottom(marker));
this.marker = marker;
overlayItems = new ArrayList<OverlayItem>();
// image to show when you drag pointer on screen to other location
dragImage = (ImageView)findViewById(R.id.drag_image);
// if(dragImage != null && dragImage.getDrawable() != null)
{
xDragImageOffset = getResources().getDrawable(R.drawable.map_icon_corn).getIntrinsicWidth() / 2;
yDragImageOffset = getResources().getDrawable(R.drawable.map_icon_corn).getIntrinsicHeight();
}
populate();
}
我已经使用这个类实现了所有覆盖。我还使用相同的覆盖类显示用户的当前位置。
这个覆盖类的 onTouch 是:
public boolean onTouchEvent(MotionEvent motionEvent, MapView mapView)
{
//System.out.println(" --- inside onTouchEvent --- "+dontAddtap);
final int x = (int)motionEvent.getX();
final int y = (int)motionEvent.getY();
boolean result = false;
// TODO Auto-generated method stub
// if(dontAddtap)
{
if(motionEvent.getAction() == MotionEvent.ACTION_DOWN && !isActionDown)
{
//System.out.println(" ------ on key down ------ geo point "+pt1);
if(!isTap)
{
for(OverlayItem item : overlayItems)
{
Point p = new Point();
mapView.getProjection().toPixels(item.getPoint(), p);
item.setMarker(marker);
boolean isHit = hitTest(item, marker, x - p.x, y - p.y);
//System.out.println(" --- isHit in MotionEvent.ACTION_DOWN --- "+isHit);
if(isHit)
{
isItemHit = true;
//System.out.println(" --- item hit ---- ");
result = true;
inDrag = item;
overlayItems.remove(inDrag);
gotGeoPoint = item.getPoint();
//System.out.println( " -------- imporntent--item--otuched point -------- "+item.getPoint());
populate();
xDragTouchOffset = 0;
yDragTouchOffset = 0;
setDragImagePosition(p.x, p.y);
dragImage.setVisibility(View.VISIBLE);
// Point p11=new Point(p.x, p.y);
xDragTouchOffset = x - p.x;
yDragTouchOffset = y - p.y;
OverlayItemArryList.remove(item);
//
mapController_.stopAnimation(true);
break;
}
}
}
}
}
if(dontAddtap && isItemHit)
{
if(motionEvent.getAction() == MotionEvent.ACTION_MOVE && inDrag != null)
{
//System.out.println(" --- onTouchEvent ACTION_MOVE --- ");
setDragImagePosition(x, y);
result = true;
isTap = isActionDown = false;
}
}
if(dontAddtap || isItemHit)
{
if(motionEvent.getAction() == MotionEvent.ACTION_UP && inDrag != null)
{
//System.out.println(" --- onTouchEvent ACTION_UP --- ");
if(isItemHit)
{
dragImage.setVisibility(View.GONE);
GeoPoint pt = mapView.getProjection().fromPixels(x-xDragTouchOffset,y-yDragTouchOffset);
toDrop = new OverlayItem(pt, inDrag.getTitle(), inDrag.getSnippet());
overlayItems.add(toDrop);
//System.out.println("------------- on drag drop point -------------- "+toDrop.getPoint());
addGeo = toDrop.getPoint();
//addReferenceToOverLayList.add(toDrop);
////////System.out.println(" addReferenceToOverLayList.add "+toDrop.getPoint());
Iterator<GeoPoint> st = geoArrayist.iterator();
// update array list with new values when any of the coordinate is moved
while(st.hasNext())
{
int k = 0;
GeoPoint check = st.next();
if(check.equals(gotGeoPoint))
{
//////System.out.println("yes we got geo points");
k = geoArrayist.indexOf(gotGeoPoint);
//////System.out.println(" - item index got from the array list - "+k);
geoArrayist.set(k, addGeo);
//////System.out.println(" --- updated array --- "+geoArrayist);
}
}
dontAddtap = result = true;
isItemHit = isActionDown = false;
populate();
}
}
}
return(result || super.onTouchEvent(motionEvent, mapView));
}
而且我可以毫无问题地将标记拖到地图上的任何地方。但是,这也导致我当前的位置指针移动(如果用户试图移动它)。我只想使用相同的覆盖类,但我想修复指向我当前位置的指针,它不应该是可移动的。有没有办法找到正在触摸的覆盖项目?或者任何其他解决方法都会有很大帮助。谢谢。