我在这里有一段代码,用于绘制地图上的一组路径。我试图让地图缩放和居中,以便所有路径都可见,但我无法正常工作。这是我开始之前代码的样子:
var points = new Array();
var pointsData = [];
var pointsData = $('#master-' + r + ' > #route-data').html();
if (pointsData != '')
{
var pointsArray = JSON.parse(pointsData);
for (var p=0; p<pointsArray.length; p++) {
points.push(new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]));
}
$.OverMap.drawRoute(points);
}
else $.OverMap.drawDirections({preserveViewport:false});
在摆弄它之后:
map = this.map.map;
map.fitBounds(this.map.bounds);
zoomChangeBoundsListener =
google.maps.event.addListenerOnce(map, 'bounds_changed', function(event) {
if (this.getZoom()){
this.setZoom(16);
}
});
setTimeout(function(){google.maps.event.removeListener(zoomChangeBoundsListener)}, 2000);
// Draw route
var bounds = new google.maps.LatLngBounds();
var points = new Array();
var pointsData = [];
var pointsData = $('#master-' + r + ' > #route-data').html();
if (pointsData != '')
{
var pointsArray = JSON.parse(pointsData);
for (var p=0; p<pointsArray.length; p++) {
points.push(new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]));
bounds.extend(pointsArray[p][0], pointsArray[p][1]);
}
$.OverMap.drawRoute(points);
}
else $.OverMap.drawDirections({preserveViewport:false});
有谁可以指出我哪里出错了?
答案 0 :(得分:0)
google.maps.LatLngBounds.extend将google.maps.LatLng个对象作为参数。
不
for (var p=0; p<pointsArray.length; p++) {
points.push(new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]));
bounds.extend(pointsArray[p][0], pointsArray[p][1]);
}
代替:
for (var p=0; p<pointsArray.length; p++) {
var pt = new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]);
points.push(pt);
bounds.extend(pt);
}