ItemizedOverlay则hitTestItemizedOverlay、hitTest

2023-09-06 19:13:43 作者:男人的酒女人的泪。

我用这种方法来检测是否有人点击的叠加。现在一切正常除外点击区域太小。所以,我看着里面说的API看看一个给定的命中点是项目的标记的范围内。

I'm using this method to detect whether someone clicks on an overlay. Now everything works except for that the click area is too small. So I looked at the api which said "See if a given hit point is within the bounds of an item's marker. ".

我做了我更大的范围如下:

I made my bounds bigger like this:

Log.d("debug", "Marker propertie 1: "  + marker.getBounds());   //output:Marker propertie 1: Rect(0, 0 - 0, 0)

Rect square = new Rect(0, 0, 200, 200);
marker.setBounds(square);
Log.d("debug", "Marker propertie 2: "  + marker.getBounds());   //output:Marker propertie 2: Rect(0, 0 - 200, 200)
if (hitTest(item, marker, x-p.x, y-p.y)) {
//...
}

但改变其边界不会改变的点击区域的两种方式。可有人暗示我,我怎么能提高我可绘制的clickarea?

But both ways of changing its bounds doesn't change the click-area. Can someone hint me how I can increase the clickarea of my drawables?

推荐答案

这是谷歌地图API?如果你想延长势必可以在此改变您的扩展itemizedOverlay的则hitTest到这样的事情。

This is the google maps api? If you want to extend the bound you can overide the hitTest of your extended itemizedOverlay to something like this.

@Override
protected boolean hitTest(OverlayItem overlayItem, Drawable drawable, int x, int y) {
    Rect bounds = drawable.getBounds();
    int newLeft = (int) (200 * ((double)bounds.left / (double)bounds.width())  ) ;
    int newTop = (int) (200 * ((double)bounds.top  / (double)bounds.height()) );
    Rect square = new Rect(newLeft, newTop, 200, 200);
    return square.contains(x, y);
}

地图我似乎工作就只是测试。

Just tested on a map I have seems to work.