Android的设置监听器对谷歌地图标记 [英] Android set Listener for marker on google map

查看:134
本文介绍了Android的设置监听器对谷歌地图标记的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

这是我使用谷歌地图覆盖类。我添加了两个标记,并要一个监听器添加到这些标记。下面是我的叠加类:

 保护类MyLocationOverlay扩展com.google.android.maps.Overlay {    @覆盖
    公共布尔平局(帆布帆布,MapView类MapView类,布尔阴影,长时){
        super.draw(画布,MapView类,阴影);
        // LNG-Point转换为纬度/我们对屏幕上的坐标。
        点myScreenCoords =新点();
        MapView.getProjection()在toPixels(对,myScreenCoords)。        BMP位图= BitmapFactory.de codeResource(getResources(),R.drawable.passenger_map);        canvas.drawBitmap(BMP,myScreenCoords.x,myScreenCoords.y,NULL);
        // canvas.drawText(我在这里......,myScreenCoords.x,myScreenCoords.y,油漆);        MapView.getProjection()在toPixels(P1,myScreenCoords)。
        位图BMP1 = BitmapFactory.de codeResource(getResources(),R.drawable.driver_map);        canvas.drawBitmap(BMP1,myScreenCoords.x,myScreenCoords.y,NULL);
        // canvas.drawText(司机:我在这里......,myScreenCoords.x,myScreenCoords.y,油漆);
        返回true;
    }


解决方案

您需要使用ItemizedOverlay类的挖掘上的标记。在你需要重写


  

中的onTap()或onTouch()


其用于标记以及在地图

 公共布尔中的onTap(最终的GeoPoint P,最终MapView类MapView类){
布尔拍了拍= super.onTap(P,MapView类);
如果(窃听){
    //你想做的事,当你点击一个项目
}
其他{
    //你想做的事时,你不打一个项目
    }
返回true;

}

//必须具有此方法中,即使它没有明显做任何事

@覆盖
保护布尔中的onTap(INT指数){
    返回true;
}

这里的链接

http://developer.android.com/guide/tutorials/意见/ HELLO-mapview.html

<一个href=\"http://stackoverflow.com/questions/6327622/ontap-event-on-map-is-not-fired/6327731#6327731\">OnTap()在地图上的事件,则不会触发

<一个href=\"http://stackoverflow.com/questions/4971749/android-itemizedoverlay-ontouchevent-and-ontap-overlapping/5422563#5422563\">Android: ItemizedOverlay onTouchEvent和重叠的onTap

<一个href=\"http://stackoverflow.com/questions/3707923/show-popup-above-map-marker-in-mapview/3829582#3829582\">Show上述图形页面地图标记弹出

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;    
    }

解决方案

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

这篇关于Android的设置监听器对谷歌地图标记的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆