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?

[Product update] Infrastructure Analysis Tool is now available with Business Accounts.Learn More

x
I wear a lot of hats...

"The solutions and answers provided on Experts Exchange have been extremely helpful to me over the last few years. I wear a lot of hats - Developer, Database Administrator, Help Desk, etc., so I know a lot of things but not a lot about one thing. Experts Exchange gives me answers from people who do know a lot about one thing, in a easy to use platform." -Todd S.

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
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
Determine the Perfect Price for Your IT Services

Do you wonder if your IT business is truly profitable or if you should raise your prices? Learn how to calculate your overhead burden with our free interactive tool and use it to determine the right price for your IT services. Download your free eBook now!

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

Experts Exchange Solution brought to you by

Your issues matter to us.

Facing a tech roadblock? Get the help and guidance you need from experienced professionals who care. Ask your question anytime, anywhere, with no hassle.

Start your 7-day free trial
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
It's more than this solution.Get answers and train to solve all your tech problems - anytime, anywhere.Try it for free Edge Out The Competitionfor your dream job with proven skills and certifications.Get started today Stand Outas the employee with proven skills.Start learning today for free Move Your Career Forwardwith certification training in the latest technologies.Start your trial today
Google Apps

From novice to tech pro — start learning today.