Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How to set zoom to fit circle in Google Maps

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?

like image 725
Sergey Romanov Avatar asked Mar 19 '12 10:03

Sergey Romanov


People also ask

How do I custom zoom on Google Maps?

The default zoom level is set to 0. You can change the zoom level of the map using simple steps. Step 1 Go to Add or Edit Map page . Step 2 Select 'Default zoom level' in the 'Map Information section'.

How do I control zoom on Google Maps?

Users can zoom the map by clicking the zoom controls. They can also zoom and pan by using two-finger movements on the map for touchscreen devices.


2 Answers

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/

like image 176
Dr.Molle Avatar answered Sep 27 '22 00:09

Dr.Molle


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); 
like image 42
Sabri Aziri Avatar answered Sep 24 '22 00:09

Sabri Aziri