-->

Google Map Marker Clickable Area

2020-08-17 07:26发布

问题:

I am using this example: https://github.com/galex/android-mapviewballoons

My problem is that the clickable area is wider than the marker itself. For example, my Google Map marker is 25x25 then the clickable area would extend up to 70x70. This is a big problem for overlapping markers.

When I clicked on the tip of that arrow, onTap is activated, even though the tap area is far from the marker.

Please help me. Thanks.

回答1:

This is the default behaivior of ItemizedOverlay. 25x25 px is generally not an adquate touchable area for most human fingers.

You should override the hitTest() method if you want to modify the way an overlay item hit is tested.



回答2:

For debugging :

Try using a TouchDelegate for the View, you can specify the Touch rect for a give View

An example showing how to use the TouchDelegate :

public class TouchDelegateSample extends Activity { 
  Button mButton; 
  @Override 
  protected void onCreate(Bundle savedInstanceState) { 
    super.onCreate(savedInstanceState); 
    setContentView(R.layout.touch_delegate_view); 
    mButton = (Button)findViewById(R.id.delegated_button); 
    View parent = findViewById(R.id.touch_delegate_root); 
    // post a runnable to the parent view's message queue so its run 
after 
    // the view is drawn 
    parent.post(new Runnable() { 
      @Override 
      public void run() { 
        Rect delegateArea = new Rect(); 
        Button delegate = TouchDelegateSample.this.mButton; 
        delegate.getHitRect(delegateArea); 
        delegateArea.top -= 200; 
        TouchDelegate expandedArea = new TouchDelegate(delegateArea, 
delegate); 
        // give the delegate to an ancestor of the view we're 
delegating the 
        // area to 
        if (View.class.isInstance(delegate.getParent())) { 
          ((View)delegate.getParent()).setTouchDelegate(expandedArea); 
        } 
      } 
    }); 
  } 
} 

hitTest()

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.