Android: draw LatLngBounds on a GoogleMap object

Android: draw LatLngBounds on a GoogleMap object

Quick question: If you have a LatLngBounds variable how to transform it to a polygon and draw it on a Google Map view in your Android Application?

LatLngBounds store a rectangle by storing only 2 points from the rectangle the North East and South West, to build a new polygon you need the rest of the points.

 



....
@Override
public void onMapReady(GoogleMap googleMap) {
   // get the bounds, for this example I will get the visible region on the google map view
   LatLngBounds b = mapObj.getProjection().getVisibleRegion().latLngBounds;

   PolygonOptions po = new PolygonOptions();

   LatLng northwest = new LatLng(b.southwest.latitude, b.northeast.longitude);
   LatLng southeast = new LatLng(b.northeast.latitude, b.southwest.longitude);

   po.add(b.northeast, northwest);
   po.add(northwest, b.southwest);
   po.add(b.southwest, southeast);
   po.add(southeast, b.northeast);

   po.strokeColor(Color.RED);

   googleMap.addPolygon(po);
}
...


No comments.