对onTap感到困惑

时间:2010-12-18 09:24:18

标签: android

我对onTap方法以及它们如何使用ItemizedOverlay感到有点困惑。我希望用户点击地图,然后在他们点击的位置放置一个OverlayItem(图标)。然后我希望他们再次点击OverlayItem以确认位置。

我可以通过覆盖

将OverlayItem添加到地图中没有问题
public boolean onTap(GeoPoint p, MapView mapView)

但是我想通过覆盖

捕获用户点击该项目
protected boolean onTap(int i)

麻烦的是,当我覆盖这两种方法的两种方法时,当我点击我的图标项时,第二种方法永远不会被执行。

我已经按照所有的例子,但我仍然被卡住了。有人可以告诉我,我做错了吗?

由于

2 个答案:

答案 0 :(得分:1)

感谢您的回复。 super.onTap似乎是我失踪的东西。这些是我的方法,所以你看到onTap的'GeoPoint'版本是主要入口点,它可以工作!我发布的链接也非常有用。

    @Override
    public boolean onTap(GeoPoint p, MapView mapView) {
        // If it was the parent that was tapped, do nothing
        if(super.onTap(p, mapView)) {
            return true;
        }
        else {
            lastClickedLocation = p;
            mapView.getController().animateTo(lastClickedLocation);                                 
            if(markerItem == null) {
                markerItem = new OverlayItem(lastClickedLocation, "", "");
                items.add(markerItem);
            }
            else {
                items.remove(markerItem);
                markerItem = new OverlayItem(lastClickedLocation, "", "");
                items.add(markerItem);
            }
            Utils.alert(parent.getApplicationContext(), parent.getResources().getString(R.string.tapAgainToConfirm));
            populate();
            return true;
        }           
    }

    /**
     * Override the onTap(index) method
     */
    @Override
    protected boolean onTap(int i) {
        parent.confirmLocation(items.get(i));
        return true;
    }

答案 1 :(得分:0)

我没试过这个,但我认为它会起作用:

尝试从GeoPoint - 风格的onTap()链接到超类。这应该为您提供调用索引风格onTap()的默认处理。覆盖该方法以找出用户何时点击现有标记并返回true以指示您处理该事件。返回GeoPoint - 风格onTap(),如果super.onTap()返回true,则表示该事件由某人(例如您)处理。因此,如果是false,请在提供的GeoPoint添加标记。

相关问题