Проблема заключается в следующем: мы устанавливаем
var bounds = new google.maps.LatLngBounds();
, чтобы мы могли позже разместить наши маркеры в ограниченной области на карте. GMaps всегда будет уменьшать масштаб асинхронно, чтобы соответствовать fitBounds () соответственно, но не будет увеличивать для достижения того же (как ранее отмечалось @broady). Это не идеально для многих приложений, так как после того, как вы ушли и отобразили серию маркеров на карте, которые вызвали уменьшение карты (возможно, <10), она не будет увеличиваться без того, чтобы пользователь не сделал этого вручную. </p>
GMaps будет по-прежнему использовать границы (нехватки лучших слов) наиболее уменьшенного статуса коллекции маркеров (извините). Установка 'null' перед каждым вызовом новых маркеров дает вам новую карту для работы.
Для автоматического увеличения просто установите LatLngBounds (); 'null', например, так (см. псевдо-пример ниже, чтобы увидеть его размещение):
bounds = new google.maps.LatLngBounds(null);
Псевдо пример:
// map stuff/initiation
...
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);
}
....
// here's an AJAX method I use to grab marker coords from a database:
$.ajax({
beforeSend: function() {
clear_markers(gmarkers); // see below for clear_markers() function declaration
},
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'];
// Plot the markers
for (r in results) {
if (r < (data['count'])) {
CreateMarker(results[r]);
}
}
}
}
},
complete: function() {
map.fitBounds(bounds);
}
});
// clear_markers()
function clear_markers(a) {
if (a) {
for (i in a) {
a[i].setMap(null);
}
a.length = 0;
}
bounds = new google.maps.LatLngBounds(null); // this is where the magic happens; setting LatLngBounds to null resets the current bounds and allows the new call for zoom in/out to be made directly against the latest markers to be plotted on the map
}