我有一个脚本从html获取数据并使用它来绘制地图上的路线。我试图让它采取所有路线和缩放显示所有这些。这是代码:
var ombounds = new google.maps.LatLngBounds(); //bounds
for (var r=1; $("#master-master > div").length; r++)
{
// Add a placemark at the start of each route
var placemark_id = "#master-" + r + " > #placemarks-data > #placemark-0";
var markerParameters = {};
markerParameters.index = r-1;
markerParameters.total = $(placemark_id + " > .total").html();
markerParameters.mapData = $(placemark_id + " > .map").html();
markerParameters.instructionsData = $("#master-" + r + " > #snippet").html();
markerParameters.distanceData = $("#master-" + r + " > #distance").html();
markerParameters.durationData = $("#master-" + r + " > #duration").html();
markerParameters.titleData = $("#master-" + r + " > #title").html();
markerParameters.urltitleData = $("#master-" + r + " > #url-title").html();
markerParameters.imageData = $(placemark_id + " > .image").html();
$.OverMap.addMarker(markerParameters);
// Draw route
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++) {
var pt = new google.maps.LatLng(pointsArray[p][0], pointsArray[p][1]);
points.push(pt);
ombounds.extend(pt); //bounds
}
$.OverMap.drawRoute(points);
}
else $.OverMap.drawDirections({preserveViewport:false});
}
(map.getBounds()); //bounds
map.fitBounds(ombounds); //bounds
由于某种原因,map.fitBounds(ombounds)什么都不做。代码仍然会绘制路由,但我无法看到边界相关的函数正在做什么。我已经添加了与缩放相关的所有行的界限 - 目前,它们对代码的输出没有明显的影响。谁知道我哪里出错?
答案 0 :(得分:1)
当路线服务的preserveViewport
选项设置为false时,对DirectionsRenderer的每次调用都会缩放地图并将地图置于其结果中心。如果您希望fitBounds覆盖它,则需要将其设置为true。