1

我这里有一段代码在地图上绘制一组路径。我试图让地图缩放和居中,以便所有路径都可见,但我什么也做不了。这是我开始之前的代码:

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});

谁能指出我哪里出错了?

4

1 回答 1

0

google.maps.LatLngBounds.extendgoogle.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);
}
于 2013-06-28T19:16:38.723 回答