Setbounds on google api v3 maps

I am trying to set the borders of the map. Fitbounds does not work because it puts some space around the borders, therefore does

{{{map.fitBounds(map.getBounds())}}} 

quickly scales the map several times

I need to be able to do

 {{{map.setBounds(map.getBounds())}}} 

and nothing will work

note that I am using v3 from the api and therefore do not have access to getBoundsZoomLevel

+6
google-maps bounds setbounds
source share
3 answers

If I'm not mistaken, I assume that you want all of your points to be visible on the map with the highest possible zoom level. I did this by initializing the map zoom level to 16 (not sure if this is the highest zoom level on V3).

 var map = new google.maps.Map(document.getElementById('map_canvas'), { zoom: 16 , center: marker_point , mapTypeId: google.maps.MapTypeId.ROADMAP }); 

Then after that I did the necessary things:

 var bounds = new google.maps.LatLngBounds(); //you can have a loop here of all you marker points //begin loop bounds.extend(marker_point); //end loop map.fitBounds(bounds); 

Result: Success!

+4
source share

FitBounds , as you say, expands the borders that exceed the borders of the second card, to make the borders the same, you can set the center and scale.

 map2.setCenter(map.getCenter()); map2.setZoom(map.getZoom()); 

Like in this JSFiddle - http://jsfiddle.net/KmNaX/

0
source share

Hey Guiz, when nothing worked for me, I planned to use this approach .. and it worked for me .. I took the coordinates from the viewport and then matched the same with the limitations .. this approach works.

 function codeAddress() { var address = document.getElementById('address').value; if(address == null || address== "") { alert('Address field is Empty.. !'); } else { geocoder.geocode({ 'address' : address }, function(results, status) { if (status == google.maps.GeocoderStatus.OK) { // If geocoder status is OK then locate the geometry,.. var location = results[0].geometry.location // Obtaining the lat lang of new position on map.. var latlng = new google.maps.LatLng(location.lat(),location.lng()); map.setCenter(location); // view port gives us the map bounds for a particular location.. **var viewportloc = String(results[0].geometry.viewport); var res = viewportloc.replace('((','').replace('))','').replace('(','').replace(')','').split(','); //alert(res[0]+" - "+res[1]+" - "+res[2]+" - "+res[3]); var sw = new google.maps.LatLng(res[0],res[1]); var ne = new google.maps.LatLng(res[2],res[3]); var bounds = new google.maps.LatLngBounds(); bounds.extend(sw); bounds.extend(ne); map.fitBounds(bounds);** addMarker(location); setonMap(map); } else { document.getElementById('address').value = ""; alert('Geocode was not successful for the following reason: ' + status); } }); } } 
0
source share

All Articles