Implementing polygons with Google maps API V2 in Android

本小妞迷上赌 提交于 2019-12-04 21:25:16

Try with this for Polygon and read this link.

https://developers.google.com/maps/documentation/android/reference/com/google/android/gms/maps/model/Polygon

GoogleMap map;

  Polygon polygon = map.addPolygon(new PolygonOptions()
 .add(new LatLng(0, 0), new LatLng(0, 5), new LatLng(3, 5), new LatLng(0, 0))
 .strokeColor(Color.RED)
 .fillColor(Color.BLUE));

For now you will have to use OnMapClickListener and implement "point inside polygon" algorithm yourself (there are implementations easy to find with google).

You need to check the orientation on every 2 consecutive points of the polygon with the one that responds to the user click. This is done with checking the sign of the determinant of the matrix made from the 3 points with a third coordinate 0. Take this function for example:

public boolean orientation(float lat1, float lon1, float lat2, float lon2, float lat3, float lon3){
     float result = lat1*lon2 + lat2*lon3 + lat3*lon1 - lon2*lat3 - lon3*lat1 - lon1*lat2;
     if(result >= 0) return true;
     else return false;
}

When you call the function for all points of the polygon that form a side and all of them return the same result - the point is inside of the polygon.

易学教程内所有资源均来自网络或用户发布的内容,如有违反法律规定的内容欢迎反馈
该文章没有解决你所遇到的问题?点击提问,说说你的问题,让更多的人一起探讨吧!