【问题标题】:Google Maps API fitBounds doesn't set zoom correctlyGoogle Maps API fitBounds 未正确设置缩放
【发布时间】:2013-03-28 22:58:56
【问题描述】:
【问题讨论】:
标签:
google-maps-api-3
fitbounds
【解决方案1】:
我通过以下方式设法解决了这个问题:
var minlatminlong = new google.maps.LatLng({{ min_latitude }}, {{ min_longitude }});
var minlatmaxlong = new google.maps.LatLng({{ min_latitude }}, {{ max_longitude }});
var maxlatminlong = new google.maps.LatLng({{ max_latitude }}, {{ min_longitude }});
var maxlatmaxlong = new google.maps.LatLng({{ max_latitude }}, {{ max_longitude }});
zoomChangeBoundsListener = google.maps.event.addListener(map, 'bounds_changed', function(){
var bounds = this.getBounds();
if ((bounds.getSouthWest().lng() > {{ min_longitude }}) || (bounds.getNorthEast().lng() < {{ max_longitude }}) || (bounds.getSouthWest().lat() > {{ min_latitude }}) || (bounds.getNorthEast().lat() < {{ max_latitude }})) {
this.setZoom(this.getZoom() - 1);
} else {
google.maps.event.removeListener(zoomChangeBoundsListener);
}
});
请注意,包含在 '{{' 和 '}}' 中的任何内容都是我传递给 html 模板的变量。