1

I'm using the following code to zoom and show all markers on a GoogleMap in Android. It works well:

LatLngBounds.Builder builder = new LatLngBounds.Builder();

//the include method will calculate the min and max bound.
builder.include(marker1.getPosition());
builder.include(marker2.getPosition());
builder.include(marker3.getPosition());
builder.include(marker4.getPosition());

LatLngBounds bounds = builder.build();

int width = getResources().getDisplayMetrics().widthPixels;
int height = getResources().getDisplayMetrics().heightPixels;
int padding = (int) (width * 0.10); // offset from edges of the map 10% of screen

CameraUpdate cu = CameraUpdateFactory.newLatLngBounds(bounds, width, height, padding);

mMap.animateCamera(cu);

But what I want to do is map all the markers in a bounding box and then get the width of that box. How can I do that?

Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
MaxSped
  • 117
  • 1
  • 1
  • 15

1 Answers1

0

You need to use bounds.northeast and bounds.southwest coordinates

Bounds dimensions

and find distance between them using Location.distanceBetween() method or any other:

float[] results = new float[1];
Location.distanceBetween(bounds.northeast.latitude, bounds.southwest.longitude,
            bounds.northeast.latitude, bounds.northeast.longitude,
            results);
float width_in_meters = results[0];
Andrii Omelchenko
  • 13,183
  • 12
  • 43
  • 79
  • @Speedy As in your code example: `builder.include(marker1.getPosition());`... Then `LatLngBounds bounds = builder.build();` and you can use `bounds.northeast` and `bounds.southwest`. – Andrii Omelchenko Nov 06 '20 at 08:58