Thursday, June 24, 2010

Re: [android-developers] Re: ItemizedOverlay.hitTest incorrect

As explained in your other email thread, it should not be necessary to
call hitTest manually for this purpose.

However, for the record, I believe the problem here is that hitTest()
takes the hit point in coordinates relative to the marker, not in
absolute screen coordinates. This is vaguely described in the
Javadoc:

See if a given hit point is within the bounds of an item's marker.
Override to modify the way an item is hit tested. **The hit point is
relative to the marker's bounds.** The default implementation just
checks to see if the hit point is within the touchable bounds of the
marker.

So to achieve what you were after here, you'd need to do something like this:

Point itemPoint = mapView.getProjection().toPixels(item.getPoint(), null);
int relativeX = screenPoint.x - itemPoint.x;
int relativeY = screenPoint.y - itemPoint.y;
if (hitTest(item, marker, relativeX, relativeY)) {
// ...
}

Steve


On Mon, Jun 21, 2010 at 2:12 PM, tfriest <tfriest@kc.rr.com> wrote:
> I did change the getBounds to copyBounds since it is offsetting the
> bounds...
>
> On Jun 16, 12:47 pm, tfriest <tfri...@kc.rr.com> wrote:
>> It appears that the hitTest method is not working right when I iterate
>> through the items in myItemizedOverlayand call hittest on them in
>> theonTap(GeoPoint, MapView) method.
>>
>> I am doing this because I want to handle taps both when they are on an
>> item and not (where I create a new item).  If I override bothonTap
>> methods theonTap(int) never gets called...
>>
>> Anyway, the problem I have with hitTest is that it only returns a hit
>> if you tap the upper left corner of the display (where there is no
>> item).
>>
>> I wrote the following in theonTapmethod
>>
>> for (OverlayItem item : items) {
>>         android.graphics.Point itemPoint =
>> mapView.getProjection().toPixels(item.getPoint(), null);
>>         android.graphics.Rect bounds = item.getMarker(0).getBounds();
>>         bounds.offset(itemPoint.x, itemPoint.y);
>>         if (bounds.contains(screenPoint.x, screenPoint.y)) {
>>                 .<handle item tapped>
>>                 .return true;
>>         }
>>
>> } // for
>>
>> I suspect that the hitTest method is not offsetting the marker to the
>> location of the item...
>> Since the arguments of the hitTest don't take the MapView or
>> Projection, I don';t see how it can work right...
>
> --
> You received this message because you are subscribed to the Google
> Groups "Android Developers" group.
> To post to this group, send email to android-developers@googlegroups.com
> To unsubscribe from this group, send email to
> android-developers+unsubscribe@googlegroups.com
> For more options, visit this group at
> http://groups.google.com/group/android-developers?hl=en
>

--
You received this message because you are subscribed to the Google
Groups "Android Developers" group.
To post to this group, send email to android-developers@googlegroups.com
To unsubscribe from this group, send email to
android-developers+unsubscribe@googlegroups.com
For more options, visit this group at
http://groups.google.com/group/android-developers?hl=en

No comments:

Post a Comment