android谷歌地图叠加ontap

时间:2012-02-12 20:15:37

标签: android google-maps map touch overlay

我创建了3个班级。他们每个都扩展Overlay。并且他们每个人都有“复杂”的覆盖绘制方法。这就是为什么我选择覆盖Overlay而不是使用ItemizedOverlay。现在,为了确定已经点击了哪个叠加层,将更容易使用ItemizedOverlay,但我仍然使用“普通”叠加层。如何正确确定我的哪些叠加层被点击。

我在扩展Overlay的每个(三个)类中都覆盖了onTap方法。结果就是无论在地图上的哪个地方,我都会触摸所有三个类的onTap。

如果我触摸了我的绘图,是否必须根据onTap的方法参数GeoPoint和我当前的位置进行计算?怎么做得好?将所有内容更改为ItemizedOverlay?如果是这样,如何做一些叠加的复杂图纸?

10倍 此致

1 个答案:

答案 0 :(得分:2)

我创建了解决方案:

@Override
public boolean onTap(GeoPoint geoPoint, MapView mapView) {
    // Gets mapView projection to use it with calculations
    Projection projection = mapView.getProjection();
    Point tapPoint = new Point();
    // Projects touched point on map to point on screen relative to top left corner
    projection.toPixels(geoPoint, tapPoint);

    // Gets my current GeoPoint location
    GeoPoint myLocationGeoPoint = getLocationGeoPoint();

    // Image on the screen width and height
    int pinWidth = myPin.getWidth();
    int pinHeight = myPin.getHeight();

    // Variable that handles whether we have touched on image or not
    boolean touched = false;

    if (myLocationGeoPoint != null) {
        Point myPoint = new Point();

        // Projects my current point on map to point on screen relative to top left corner
        projection.toPixels(myLocationGeoPoint , myPoint);

        // Because image bottom line is align with GeoPoint and this GeoPoint is in the middle of image we have to do some calculations
        // absDelatX should be smaller that half of image width because as I already said image is horizontally center aligned
        int absDeltaX = Math.abs(myPoint.x - tapPoint.x);
        if (absDeltaX < (pinWidth / 2)) {
            // Because image is vertically bottom aligned with GeoPoint we have to be sure that we hace touched above GeoPoint (watch out: points are top-left corner based)
            int deltaY = myPoint.y - tapPoint.y;
            if (deltaY < pinHeight) {
                // And if we have touched above GeoPoint and belov image height we have win!
                touched = true;
            }
        }
    }

    if (touched) {
        Log.d(TAG, "We have been touched! Yeah!");
    }

    return true;
}