Saturday 15 May 2010

javascript - Google maps api: map not consistently executing fitBounds -



javascript - Google maps api: map not consistently executing fitBounds -

code in question:

function setallmap(map) { console.debug(3); (var = 0; < markers.length; i++) { markers[i].setmap(map); } resetzoomonmapevents(markers); } function resetzoomonmapevents(nuloc) { console.debug(1); var marks = typeof nuloc == 'undefined' ? nuloc : markers; var bounds = new google.maps.latlngbounds(); (var in marks) { bounds.extend(marks[i].position); } map.setcenter(bounds.getcenter()); map.fitbounds(bounds); }

the map works correctly on homecoming of search results. center center of map markers other times center on spain/africa. direct question: why behavior not consistent?

perhaps sharing script(s) help trigger something? have loop, each position, phone call method called "rendermarker". method assembles marker.

i phone call "centermap" method.

var googlemaps = { 'map': {}, 'marker': {}, 'markerarray': [], 'markerlatlngarray': [], /** * renders marker(s) map. * creates infowindow given marker. * * @param {object} myobj| info given marker. * @returns {void} * */ rendermarker: function (myobj) { var markerlatlng = new google.maps.latlng(myobj.lat, myobj.lng); googlemaps.marker = new google.maps.marker({ 'position': markerlatlng, 'map': googlemaps.map, 'title': my_title, 'icon': 'my-icon.png', 'animation': google.maps.animation.drop, 'data': '<div class="myobjinfowindow">' '<p>whatever want goes here.</p>' + '</div>' }); googlemaps.marker.set('propertya', myobj.propertya); googlemaps.marker.set('propertyb', myobj.propertyb); googlemaps.marker.set('propertyc', myobj.propertyc); googlemaps.markerlatlngarray.push(markerlatlng); googlemaps.markerarray.push(googlemaps.marker); googlemaps.centermap(); googlemaps.infowindow = new google.maps.infowindow(); google.maps.event.addlistener(googlemaps.marker, 'click', function () { googlemaps.infowindow.close(); googlemaps.infowindow.setcontent(this.data); googlemaps.infowindow.open(googlemaps.map, this); }); } /** * centers map around given markers. * * @returns {void} * */ centermap: function () { var latlngbounds = new google.maps.latlngbounds(); $.each(googlemaps.markerlatlngarray, function (i, marker) { latlngbounds.extend(marker); }); googlemaps.map.setcenter(latlngbounds.getcenter()); googlemaps.map.fitbounds(latlngbounds); } }

javascript google-maps-api-3

No comments:

Post a Comment