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.
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>
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
© 2022 - 2024 — McMap. All rights reserved.