Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Android Google Map Polygon click event [duplicate]

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 map without a problem. But isn't there any way to open like pop-up when user touches on any polygon? Or maybe if user wants to change coordinates of selected polygon. I saw many examples, but they are done with javascript or some using different third party. Do someone has any advice? Thanks in advance.

like image 480
arenko Avatar asked Jul 11 '26 20:07

arenko


1 Answers

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:

public class MySupportMapFragment extends SupportMapFragment {
private View mOriginalContentView;
private TouchableWrapper mTouchView;
private BasicMapActivity mActivity;

@Override
public void onCreate(Bundle savedInstanceState) {
    super.onCreate(savedInstanceState);
    mActivity = (BasicMapActivity) getActivity();
}

@Override
public View onCreateView(LayoutInflater inflater, ViewGroup parent,
        Bundle savedInstanceState) {
    mOriginalContentView = super.onCreateView(inflater, parent,
            savedInstanceState);
    mTouchView = new TouchableWrapper();
    mTouchView.addView(mOriginalContentView);
    return mTouchView;
}

@Override
public View getView() {
    return mOriginalContentView;
}

class TouchableWrapper extends FrameLayout {

    public TouchableWrapper() {
        super(mActivity);
    }

    @Override
  public boolean dispatchTouchEvent(MotionEvent event) {
    switch (event.getAction()) {
      case MotionEvent.ACTION_DOWN:
          break;
      case MotionEvent.ACTION_UP: {
          int x = (int) event.getX();
          int y = (int) event.getY();
          mActivity.tapEvent(x,y);
          break;
      }
    }
    return super.dispatchTouchEvent(event);
  }
}

}

BasicMapActivity:

public void tapEvent(int x, int y) {
    Log.d(TAG,String.format("tap event x=%d y=%d",x,y));
    if(!isEditMode()) {
        Projection pp = mMap.getProjection();
        LatLng point = pp.fromScreenLocation(new Point(x, y));
        for (Shape ss : mPolygons) {
            if(ss.isPointInPolygon(point)) {
                ss.mMarkers.get(0).marker.showInfoWindow();
            }
        }
    }
}

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

Layout:

<?xml version="1.0" encoding="utf-8"?>
<RelativeLayout xmlns:android="http://schemas.android.com/apk/res/android"
android:layout_width="match_parent"
android:layout_height="match_parent" >

<fragment
    android:id="@+id/map"
    android:layout_width="match_parent"
    android:layout_height="match_parent"
    class="au.com.datalink.plugins.MySupportMapFragment" />

</RelativeLayout>
like image 91
Mixaz Avatar answered Jul 13 '26 13:07

Mixaz



Donate For Us

If you love us? You can donate to us via Paypal or buy me a coffee so we can maintain and grow! Thank you!