Implementing polygons with Google maps API V2 in A

2020-08-04 09:54发布

问题:

I implemented the Google maps API V2 on Android, I got the map to show and also the polygon to show using this code:

public class MainActivity extends FragmentActivity {
Polygon polygon;
@Override
protected void onCreate(Bundle savedInstanceState) {

super.onCreate(savedInstanceState);
setContentView(R.layout.activity_main);

GoogleMap googleMap;
googleMap = ((SupportMapFragment)(getSupportFragmentManager().findFragmentById(R.id.map))).getMap();
LatLng latLng = new LatLng(35.20418,-90.08342);
googleMap.setMapType(GoogleMap.MAP_TYPE_NORMAL);
googleMap.addMarker(new MarkerOptions()
        .position(latLng)
        .title("My Spot")
        .snippet("This is my spot!")
        .icon(BitmapDescriptorFactory.defaultMarker(BitmapDescriptorFactory.HUE_AZURE)));
googleMap.getUiSettings().setCompassEnabled(true);
googleMap.getUiSettings().setZoomControlsEnabled(true);
googleMap.animateCamera(CameraUpdateFactory.newLatLngZoom(latLng, 10));

int status = GooglePlayServicesUtil.isGooglePlayServicesAvailable(getApplicationContext());
if(status == ConnectionResult.SUCCESS) {
//Success! Do what you want

Log.i("service", "Google play available");
}
else{
Log.i("service", "No Google play available");
}

PolygonOptions rectOptions = new PolygonOptions()
.add(new LatLng(35.25010,-90.08342),
 new LatLng(35.25010,-90.04175),
 new LatLng(35.29177,-90.04175),
 new LatLng(35.29177,-90.08342),
 new LatLng(35.25010,-90.08342));


//Set the rectangle's stroke color to red
 rectOptions.strokeColor(Color.BLUE);
//Set the rectangle's fill to blue
rectOptions.fillColor(Color.CYAN);
rectOptions.strokeWidth(2);
//Get back the mutable Polygon
polygon = googleMap.addPolygon(rectOptions);

}

}

On the Google map API V1 the onTap method is used to interact with the polygon. My question is what's the best way to get onTap events in version2? I've read the Google docs and unless I missed it, I did not see anything on it.

回答1:

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));


回答2:

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



回答3:

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.