28

We have distance search filter. It has a map viewport that allow to set base marker and a input text box that allows enter distance in kilometers.

We then add a circle to show this distance on the map.

How can I zoom the map so it fits the circle?

ashleedawg
  • 20,365
  • 9
  • 72
  • 105
Sergey Romanov
  • 2,949
  • 4
  • 23
  • 38

2 Answers2

63

A google.maps.Circle has a getBounds() method which returns the LatLngBounds of the circle. You may use this bounds as argument for google.maps.Map.fitBounds()

If using a circle, you can do this:

map.fitBounds(circle.getBounds());

...at the end of the init-function.

http://jsfiddle.net/doktormolle/MHLjy/

Darryl Hein
  • 142,451
  • 95
  • 218
  • 261
Dr.Molle
  • 116,463
  • 16
  • 195
  • 201
27

For multiple circles use union instead of extend:

var bounds = new google.maps.LatLngBounds();

$.each(circles, function(index, circle){
    bounds.union(circle.getBounds());
});

map.fitBounds(bounds);
Sabri Aziri
  • 4,084
  • 5
  • 30
  • 45
  • 2
    Nice. I've used this to ensure that *at least* the search radius circle is encompassed, then extended the bounds for any markers further out with latlngBounds.extend(marker.getPosition()) – Jon Aug 05 '16 at 16:26