Google Map Javascript FitBounds on Markers Too Far Out

Multi tool use
Multi tool use


Google Map Javascript FitBounds on Markers Too Far Out



I have a Google Javascript API map that works relatively okay, I even managed to get the pan to bounds working. But to me, I feel as if the fitBounds action in my below script isn't working quite correctly.



At the moment here is the map portion of my script:


var map = new google.maps.Map(document.getElementById('map'), {
});
var bounds = new google.maps.LatLngBounds();

$.each(data[1].locations, function(i, item) {

var marker = new google.maps.Marker({
position: {lat: parseFloat(item.latitude), lng: parseFloat(item.longitude)},
map: map,
title: ''
});
var loc = new google.maps.LatLng(marker.position.lat(), marker.position.lng());
bounds.extend(loc);

});

map.fitBounds(bounds); // auto-zoom//
map.panToBounds(bounds); // auto-center//



In it, I correctly place a few markers, and pan to their general location. Everything shows up correctly but it shows up as shown below:



enter image description here



This feels a little too zoomed out to me, and was wondering if there was an option to get closer, or maybe to reformat my script to get the above output into something like this:



enter image description here





Try to add zoom. May be this will help var map = new google.maps.Map(document.getElementById('map'), {zoom: 6});
– Empty Brain
Jul 3 at 6:08



var map = new google.maps.Map(document.getElementById('map'), {zoom: 6});





@EmptyBrain - that looks like that solved it. Will it still automatically zoom or will it stay at the default of 6? Thanks so much for your help!
– Matthew
Jul 3 at 6:18





Map will load with this zoom value. you can change it. May be this doc will help DOC
– Empty Brain
Jul 3 at 6:22









By clicking "Post Your Answer", you acknowledge that you have read our updated terms of service, privacy policy and cookie policy, and that your continued use of the website is subject to these policies.

x8KyKVuyvT x,hBL03mj
sStj0 oF u9k,wxKSXosXUJ3z7kPkp9S iQhoaAo5cR2th87 iqVo4T,UuJfJd bRIw3voOuG

Popular posts from this blog

PHP contact form sending but not receiving emails

Do graphics cards have individual ID by which single devices can be distinguished?

Create weekly swift ios local notifications