简体   繁体   中英

Google Maps - zoom to polygon

I have checked several questions ( this one , this one and this one ) concerning zooming the google map to a given Polygon or List<LatLng> in Android, but I haven't been able to find an answer.

What would a function

public static int getZoomLevelForPolygon(final List<LatLng> listOfPolygonCoordinates)

look like? Is there a chance I can zoom the map to a certain polygon?

Ok I think I managed to find a solution: First, we generate a minimal rectangle which can fit the polygon, also known as the LatLngBounds object. Then, we move the camera to fit the LatLngBounds provided as an argument.

Main call:

  final int POLYGON_PADDING_PREFERENCE = 200;
  final LatLngBounds latLngBounds = getPolygonLatLngBounds(polygon);
  googleMap.moveCamera(CameraUpdateFactory.newLatLngBounds(latLngBounds, POLYGON_PADDING_PREFERENCE));

Helper function:

  private static LatLngBounds getPolygonLatLngBounds(final List<LatLng> polygon) {
    final LatLngBounds.Builder centerBuilder = LatLngBounds.builder();
    for (LatLng point : polygon) {
        centerBuilder.include(point);
    }
    return centerBuilder.build();
  }

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM