这是问题的症结所在:我们设置了
var bounds = new google.maps.LatLngBounds();
为了使我们的标记适应地图上的有界区域,GMaps将始终异步缩小以相应地适应fitBounds(),但不会缩小以实现相同的效果(如@broady之前所述)。对于许多应用程序来说,这并不理想,因为一旦您在地图上显示了一系列标记导致地图缩小(可能<10),它将不会自动缩回到原来的大小,除非用户手動操作。
GMaps将继续使用(最合适的词)最缩小的标记集合状态的范围。在每次调用新标记之前将其设置为“null”将为您提供一个全新的地图。
要自动缩放,请简单地将LatLngBounds();设置为“null”,如下面的伪代码示例所示:
bounds = new google.maps.LatLngBounds(null);
伪代码示例:
...
var bounds = new google.maps.LatLngBounds();
var gmarkers = [];
function CreateMarker (obj) {
myLatLng = new google.maps.LatLng(obj['latitude'], obj['longitude']);
marker = new google.maps.Marker({
position: myLatLng,
map: map
});
google.maps.event.addListener(marker, 'click', (function(marker, i) {
return function() {
infowindow.setContent(obj['job']);
infowindow.open(map, marker);
}
})(marker, i));
bounds.extend(myLatLng);
gmarkers.push(marker);
}
....
$.ajax({
beforeSend: function() {
clear_markers(gmarkers);
},
cache: false,
data: params,
dataType: 'json',
timeout: 0,
type: 'POST',
url: '/map/get_markers.php?_=<?php echo md5(session_id() . time() . mt_rand(1,9999)); ?>',
success: function(data) {
if (data) {
if (data['count'] > 0) {
var obj;
var results = data['results'];
for (r in results) {
if (r < (data['count'])) {
CreateMarker(results[r]);
}
}
}
}
},
complete: function() {
map.fitBounds(bounds);
}
});
function clear_markers(a) {
if (a) {
for (i in a) {
a[i].setMap(null);
}
a.length = 0;
}
bounds = new google.maps.LatLngBounds(null);
}
fitBounds
,panToBounds
应该做什么。 - dbkaplun