I can't set the bounds of a map properly

I can't set the bounds of a map properly with multiple markers. Here is a section of my function. "markers" is a global variable not shown. I'm not sure if it's current to push dmarker on to an array is valid. Also note, this code is part of a jquery .each(), so it will often execute more than once.

geocoder.geocode({ 'address': $(liElems[1]).text() }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var dmarker = new google.maps.Marker({ map: map, position: results[0].geometry.location });
                    markers.push(dmarker);
                    var bounds = new google.maps.LatLngBounds();
                    for (i = 0; i < markers.length; i++) {
                        bounds.extend(markers[i].getPosition());
                        alert(markers[i].getPosition());
                    }
                    map.fitBounds(bounds);
                }
                else {
                    alert(status);
                }
            });

Open in new window


The proper address info is being passed and all markers show up in their correct place. This part is the problem:
for (i = 0; i < markers.length; i++) {
      bounds.extend(markers[i].getPosition());
 }

Open in new window


It's telling me that undefined is not a function, so something is not right about the way I am creating the bounds variable, even though the debugger tells me that bounds has a value
LVL 9
BobHavertyComhAsked:
Who is Participating?
 
Tom BeckCommented:
Hold on. Just saw something. In setMarkerPosition() you are not saving a Google Maps marker object to the markers array, only a Google Maps LatLng object so neither
bounds.extend(markers[i].position);

Open in new window

nor
bounds.extend(markers[i].getPosition());

Open in new window

are going to work when that LatLng object is encountered in the for loop.
0
 
Tom BeckCommented:
This line is the problem.

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

Actually, not the line, but where you have it. You are creating a new bounds object with each geocode. The variable bounds needs to be created ONCE, outside of any function, then referred to inside the function. That way you are always referring to the same bounds instance when extending the map to fit the bounds.
0
 
BobHavertyComhAuthor Commented:
Hi Tom, glad to see you. What you said makes sense, but it still doesn't work but it's getting much closer. This line gives undefined not a function
"bounds.extend(markers[i].getPosition());"

Open in new window


Below is my code altered as you suggested.

global variables
var map, currentPositionMarker, mapCenter = new google.maps.LatLng(0, 0);
var markers = new Array();
var bounds = new google.maps.LatLngBounds();

Open in new window


Called function
function getDeliveries() {
    var msg = encodeURI("driverDeliveries.aspx");
    $.get(msg, function (data) {
        $("<div>" + data + "</div>").find("ul").each(function (intIndex, objUL) {
            var liElems = $(objUL).find('li');
            geocoder = new google.maps.Geocoder();
            geocoder.geocode({ 'address': $(liElems[1]).text() }, function (results, status) {
                if (status == google.maps.GeocoderStatus.OK) {
                    var dmarker = new google.maps.Marker({ map: map, position: results[0].geometry.location });
                    markers.push(dmarker);
                    for (i = 0; i < markers.length; i++) {
                        bounds.extend(markers[i].getPosition());
                    }
                    map.fitBounds(bounds);
                }
                else {
                    alert(status);
                }
            });
        });
    });
}

Open in new window


markers[] is first initialized with a person's location here:
function setMarkerPosition(marker, position) {
    var latlng = new google.maps.LatLng(position.coords.latitude, position.coords.longitude);
    marker.setPosition(latlng);
    markers.push(latlng);
}

Open in new window


I've looked at the array and there are two values in it, which there should be. One set for the person's current location which is initially set, and one for the location I extracted from my $.get() call, so set marker position seems to be working, because if I remove markers.push(latlng); from it, the array never gets initialized and I get a different error. But the array is being initialialized by setMarkerPosition.

Also note that both location markers show up on the map, but the zoom is set to only focus on the first lat lng initialized by the user's current location.
0
Ultimate Tool Kit for Technology Solution Provider

Broken down into practical pointers and step-by-step instructions, the IT Service Excellence Tool Kit delivers expert advice for technology solution providers. Get your free copy now.

 
Tom BeckCommented:
Might help, might not, but there's really no reason to call getPosition() since you have already saved the lat lng position in the array. This should suffice;

bounds.extend(markers[i].position);

Open in new window

0
 
BobHavertyComhAuthor Commented:
replaced LatLng with marker. It works. I knew it had something to do with the array never being properly initialized, just couldn't figure out why. Thanks a bunch!!!
0
 
Tom BeckCommented:
You're welcome. Thanks for the points.
0
Question has a verified solution.

Are you are experiencing a similar issue? Get a personalized answer when you ask a related question.

Have a better answer? Share it in a comment.

All Courses

From novice to tech pro — start learning today.