2

这是我的代码:

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  

点击监听器不是在地理点上工作吗?

4

2 回答 2

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);
于 2012-04-21T20:21:52.730 回答
0

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

于 2012-04-21T20:24:57.640 回答