2011-07-29 44 views
1

這是我在Google地圖中使用的覆蓋類。我向它添加了兩個標記,並且想要爲這些標記添加一個Listener。以下是我的覆蓋類:Android設置Google地圖上標記的監聽器

protected class MyLocationOverlay extends com.google.android.maps.Overlay { 

    @Override 
    public boolean draw(Canvas canvas, MapView mapView, boolean shadow, long when) { 


     super.draw(canvas, mapView, shadow); 
     // Converts lat/lng-Point to OUR coordinates on the screen. 
     Point myScreenCoords = new Point(); 
     mapView.getProjection().toPixels(p, myScreenCoords); 



     Bitmap bmp = BitmapFactory.decodeResource(getResources(), R.drawable.passenger_map); 

     canvas.drawBitmap(bmp, myScreenCoords.x, myScreenCoords.y, null); 
     // canvas.drawText("I am here...", myScreenCoords.x, myScreenCoords.y, paint); 

     mapView.getProjection().toPixels(p1, myScreenCoords); 
     Bitmap bmp1 = BitmapFactory.decodeResource(getResources(), R.drawable.driver_map); 

     canvas.drawBitmap(bmp1, myScreenCoords.x, myScreenCoords.y, null); 
     // canvas.drawText(" Driver : I am here...", myScreenCoords.x, myScreenCoords.y, paint); 
     return true;  
    } 

回答

1

您需要使用ItemizedOverlay類來點擊標記。在你需要重寫

中的onTap()或onTouch()

它用於標記,以及在地圖

public boolean onTap (final GeoPoint p, final MapView mapView){ 
boolean tapped = super.onTap(p, mapView); 
if (tapped){    
    //do what you want to do when you hit an item   
}   
else{ 
    //do what you want to do when you DONT hit an item 
    }     
return true; 

}

//你必須有這種方法,即使它沒有明顯的做任何事情

@Override protected boolean onTap(int index){ return true; }

這裏是鏈接

http://developer.android.com/guide/tutorials/views/hello-mapview.html

OnTap() event on map is not fired

Android: ItemizedOverlay onTouchEvent and onTap overlapping

Show popup above map marker in MapView