我正在做一个小型网络应用,我在其中使用google-maps-api-3在A点和B点之间绘制一组不同的替代路线。 我需要能够在点击事件的基础上单独突出显示每一个,但我想在地图中绘制路线时禁用自动对焦谷歌地图。
在为每条路线设置新数据之前,我首先将其从地图中删除:
for(var i=0; i < journeysContainer.JOURNEYS.length; i++) {
directionsDisplayArray[i].setMap(undefined);
}
之后,我使用此功能为我设置所有不同路线的新DirectionsRenderer选项:
function adjustRouteStroke(routeIndex) {
var strokeWeight = 3;
var strokeOpacity = 1;
var color = '#707070';
for(var i=0; i<directionsDisplayArray.length; i++) {
directionsDisplayArray[i] = new google.maps.DirectionsRenderer({
suppressMarkers: true,
suppressInfoWindows: true,
preserveViewport : true,
routeIndex: 5,
polylineOptions: {
strokeOpacity: strokeOpacity,
strokeColor: color,
strokeWeight: strokeWeight
},
markerOptions: {
icon : iconBaseURL + color
}
});
}
//highlight the selected route stroke
strokeWeight = showBalloons?7:5;
strokeOpacity = 1;
color = '#1A00DC';
directionsDisplayArray[routeIndex] = new google.maps.DirectionsRenderer({
suppressMarkers: !showBalloons,
suppressInfoWindows: true,
preserveViewport : true,
routeIndex: 1,
polylineOptions: {
strokeOpacity: strokeOpacity,
strokeColor: color,
strokeWeight: strokeWeight
},
markerOptions: {
icon : selectedMarkerIcon
}
});
}
在此之后我只用代码重绘所有路线:
for(var i=0; i < journeysContainer.JOURNEYS.length; i++) {
if(i != routeIndex){
directionsDisplayArray[i].setDirections(directionsResultArray[i]);
directionsDisplayArray[i].setMap(map);
}
}
directionsDisplayArray[routeIndex].setDirections(directionsResultArray[routeIndex]);
directionsDisplayArray[routeIndex].setMap(map);
我做错了什么?我认为preserveViewport选项足以避免更改当前地图焦点和中心。