我有一個應用程序,使用谷歌地圖。我也在地圖上覆蓋。谷歌地圖與覆蓋
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));
}
而且我可以在任何地方拖動標記在地圖上沒有任何問題。但是,這也導致我當前的位置指針移動(如果用戶試圖移動它)。我只是想使用相同的覆蓋類,但我想修復指向我當前位置的指針,它不應該是可移動的。有什麼方法可以找到正在觸摸哪個疊加項目?或者任何其他解決方法都會有很大的幫助。謝謝。
如何在構造函數中添加一個布爾值,來判斷這個標記是否是當前位置標記。然後,如果它是真的,你可以選擇不能點擊和移動!? – Carnal 2012-07-06 12:01:30