Android set Listener for marker on google map

こ雲淡風輕ζ 提交于 2019-12-24 09:47:56

问题


This is the overlay class I am using in Google Maps. I added two markers to it and want to add a Listener to these markers. Below is my overlay class:

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:


you need to use the ItemizedOverlay class for that to tap on the Marker. In that you need to override

onTap() or onTouch()

which is used for marker as well as for map

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;

}

//You must have this method, even if it doesn't visibly do anything

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

here are the links

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



来源:https://stackoverflow.com/questions/6870526/android-set-listener-for-marker-on-google-map

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