在geopoint android和谷歌地图上的onClick监听器

时间:2012-04-21 19:31:01

标签: android google-maps

这是我的代码:

geopoint =  new GeoPoint((int) (1.352566007* 1E6), (int) (103.78921587* 1E6));
        mapView.getOverlays().add( new  DrawableMapOverlay(this,geopoint,R.drawable.pushpin, "test"));
        geopoint.setOnClickListener(new OnClickListener()
          {   
              public void onClick(View v)
              {
              System.out.println("Clicked");
              }
          }); 

我收到此错误:

The method setOnClickListener(new View.OnClickListener(){}) is undefined for the type GeoPoint  

是否在点击侦听器处理地理点?

2 个答案:

答案 0 :(得分:1)

您应该扩展ItemizedOverlay<>类并覆盖onTap()方法。

这样的事情:

public class MapOverlay extends ItemizedOverlay<OverlayItem> {

    private ArrayList<OverlayItem> mapOverlay = new ArrayList<OverlayItem>();
    Context mContext;


    public MapOverlay(Drawable itemMarker) {
        super(boundCenterBottom(itemMarker));
        // TODO Auto-generated constructor stub
    }

    public void addItem(OverlayItem item){
        mapOverlay.add(item);
        populate();
    }

    @Override
    protected OverlayItem createItem(int i) {
        // TODO Auto-generated method stub
        return mapOverlay.get(i);
    }

    @Override
    public int size() {
        // TODO Auto-generated method stub
        return mapOverlay.size();
    }


    public MapOverlay(Drawable itemMarker, Context context){
        super(boundCenterBottom(itemMarker));
        mContext = context;
    }


    @Override
    protected boolean onTap(int index) {
        // TODO Auto-generated method stub
        final OverlayItem item = mapOverlay.get(index);
        AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);

        dialog.setTitle(item.getTitle());
        dialog.setMessage(item.getSnippet());

        dialog.show();

        return true;
    }
}

在活动中添加项目:

Drawable voltaIcon = this.getResources().getDrawable(R.drawable.arch);
MapOverlay volte = new MapOverlay(voltaIcon, this);

GeoPoint p_Volta = new GeoPoint(45373024, 14348799); 
OverlayItem volta = new OverlayItem(p_Volta,
            getResources().getStringArray(R.array.volta)[0], 
            getResources().getStringArray(R.array.volta)[1]);
volte.addItem(volta);

答案 1 :(得分:0)

您只能在作为View子类的对象上设置onClickListener。基本上这意味着像Button或TextView或屏幕上具有可见性的任何东西,以便您可以单击或触摸它。 GeoPoint本身没有可见性。