0

I am using google v3, i want to fitbounds with center on userPinLoc object, i have the following code

 var bounds = new google.maps.LatLngBounds();
            bounds.extend(userPinLoc)// wants to center on userPinLocation
            for (i in nearestEntitiesToZoom) {
                    entity = nearestEntitiesToZoom[i];
                    var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
                    bounds.extend(googleLatLng);
                }

                bounds.extend(userPinLoc);
                //googleMap.setCenter(userPinLoc) this not working

googleMap.fitBounds(bounds);

any quick fix after update i am pasting new code

function setInitialZoom() {
        mapZoom = googleMap.getZoom(); 
        var bounds = new google.maps.LatLngBounds();
        bounds.extend(userPinLoc);
        for (i in nearestEntitiesToZoom) {
            entity = nearestEntitiesToZoom[i];
            var googleLatLng = new google.maps.LatLng(entity.latitude,entity.longitude);
            bounds.extend(googleLatLng);
        }

        google.maps.event.addDomListener(googleMap, 'bounds_changed', function() {
            googleMap.setCenter(userPinLoc);
        });

        googleMap.fitBounds(bounds);
        setTimeout(function() {
            google.maps.event.clearListeners(googleMap, 'bounds_changed');
        }, 3000);
    }
d-man
  • 57,473
  • 85
  • 212
  • 296

1 Answers1

0

Remove the setCenter from where it is currently. You need to have an event listener for when the map's bounds change. I think when you call fitBounds, you have to wait for it to redraw before you can adjust the centre. One way would be to use a timeout, but you can simply add this to your initialize function:

google.maps.event.addDomListener(googleMap, 'bounds_changed', updateCenter);

And then create a new function to update the centre, which takes the userPinLoc value (needs to be a global variable):

function updateCenter() { 
    googleMap.setCenter(userPinLoc);
}
duncan
  • 31,401
  • 13
  • 78
  • 99
  • If not, post all your JS you're using for the Maps stuff – duncan Jan 09 '12 at 17:26
  • i see issue again, i found that making center works but if any pin goes out of context it won't set auto zoom level. i have updated my code check setInitialzoom method – d-man Jan 23 '12 at 06:22
  • You previously accepted my answer to your question. You should really post a new question for this other issue instead of just editing your original question and un-accepting my answer. – duncan Jan 23 '12 at 09:32
  • ok sorry for that :) i create new question thanks to help dear – d-man Jan 23 '12 at 09:46