Skip to content Skip to sidebar Skip to footer

Android Google Map Polygon Click Event

I am working on a map application on Android and i am using Google Maps Android API V2. I get the polygon data from a web service, convert it by XML parse and can show it on the ma

Solution 1:

I had the same problem. onMapClickListener is not called when user taps a polygon, it's only called when other overlays (such as Polygons) do not process the tap event. Polygon does process it, as you can see - GM moves the polygon to center of screen. And the event is not passed to onMapClickListener, that's it. To workaround it, I intercept tap events before GM handles them, in a View wrapping MapFragment, as described here, project clicked point from screen coordinates to map, and then check if it is inside a polygon on the map as described here (other answer tells about it too)

Relevant code:

publicclassMySupportMapFragmentextendsSupportMapFragment {
private View mOriginalContentView;
private TouchableWrapper mTouchView;
private BasicMapActivity mActivity;

@OverridepublicvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BasicMapActivity) getActivity();
}

@Overridepublic View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {
    mOriginalContentView = super.onCreateView(inflater, parent,
            savedInstanceState);
    mTouchView = newTouchableWrapper();
    mTouchView.addView(mOriginalContentView);
    return mTouchView;
}

@Overridepublic View getView() {
    return mOriginalContentView;
}

classTouchableWrapperextendsFrameLayout {

    publicTouchableWrapper() {
        super(mActivity);
    }

    @OverridepublicbooleandispatchTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          break;
      case MotionEvent.ACTION_UP: {
          intx= (int) event.getX();
          inty= (int) event.getY();
          mActivity.tapEvent(x,y);
          break;
      }
    }
    returnsuper.dispatchTouchEvent(event);
  }
}

}

BasicMapActivity:

publicvoidtapEvent(int x, int y) {
    Log.d(TAG,String.format("tap event x=%d y=%d",x,y));
    if(!isEditMode()) {
        Projectionpp= mMap.getProjection();
        LatLngpoint= pp.fromScreenLocation(newPoint(x, y));
        for (Shape ss : mPolygons) {
            if(ss.isPointInPolygon(point)) {
                ss.mMarkers.get(0).marker.showInfoWindow();
            }
        }
    }
}

protectedvoidonCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    setContentView(R.layout.activity_map);
}

Layout:

<?xml version="1.0" encoding="utf-8"?><RelativeLayoutxmlns:android="http://schemas.android.com/apk/res/android"android:layout_width="match_parent"android:layout_height="match_parent" ><fragmentandroid:id="@+id/map"android:layout_width="match_parent"android:layout_height="match_parent"class="au.com.datalink.plugins.MySupportMapFragment" /></RelativeLayout>

Solution 2:

All you have to work with is onMapClickListener which returns the latlng of the press

public abstract void onMapClick (LatLng point)

Called when the user makes a tap gesture on the map, but only if none of the overlays of the map handled the gesture. Implementations of this method are always invoked on the main thread. Parameters point The point on the ground (projected from the screen point) that was tapped.

Then check if the latlng is inside the polygon.

How to determine if a point is inside a 2D convex polygon?

I kinda pieced this together but the good news is lat and lng are already doubles. Good Luck

Post a Comment for "Android Google Map Polygon Click Event"