We help IT Professionals succeed at work.
Get Started

I can't set the bounds of a map properly

261 Views
Last Modified: 2015-04-13
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
Comment
Watch Question
CERTIFIED EXPERT
Top Expert 2015
Commented:
This problem has been solved!
Unlock 1 Answer and 6 Comments.
See Answer
Why Experts Exchange?

Experts Exchange always has the answer, or at the least points me in the correct direction! It is like having another employee that is extremely experienced.

Jim Murphy
Programmer at Smart IT Solutions

When asked, what has been your best career decision?

Deciding to stick with EE.

Mohamed Asif
Technical Department Head

Being involved with EE helped me to grow personally and professionally.

Carl Webster
CTP, Sr Infrastructure Consultant
Ask ANY Question

Connect with Certified Experts to gain insight and support on specific technology challenges including:

  • Troubleshooting
  • Research
  • Professional Opinions
Did You Know?

We've partnered with two important charities to provide clean water and computer science education to those who need it most. READ MORE