如何在计算路线后删除var位置标记(CalcRoute功能)?因此,仅存在起始和结束地址标记。在此先感谢: - )
var directionsDisplay;
var directionsService = new google.maps.DirectionsService();
var map;
function initialize() {
directionsDisplay = new google.maps.DirectionsRenderer();
var locations = [
['<strong>Scuola dell\'Infanzia e Primaria</strong><br>Via Asmara 32<br><small>Tel.: 0686219772</small>', 41.926979,12.517385, 3],
['<strong>Scuola Primaria Statale</strong><br>Via Novara 22<br><small>Tel./Fax: 068557507</small>', 41.914873,12.506486, 2],
['<strong>Scuola Secondaria di I Grado</strong><br>Via Sebenico 1<br><small>Tel./Fax: 068549282</small>', 41.918574,12.507201, 1]
];
var infowindow = new google.maps.InfoWindow();
var myOptions = {
mapTypeId: google.maps.MapTypeId.ROADMAP,
styles: [{
featureType: "poi.business",
elementType: "labels",
stylers: [{
visibility: "off"
}]
}]
}
map = new google.maps.Map(document.getElementById("map"),
myOptions);
var bounds = new google.maps.LatLngBounds();
for (i = 0; i < locations.length; i++) {
marker = new google.maps.Marker({
position: new google.maps.LatLng(locations[i][1], locations[i][2]),
map: map
});
bounds.extend(marker.position);
google.maps.event.addListener(marker, 'click', (function (marker, i) {
return function () {
infowindow.setContent(locations[i][0]);
infowindow.open(map, marker);
}
})(marker, i));
}
map.fitBounds(bounds);
var listener = google.maps.event.addListener(map, "idle", function () {
map.setZoom(15);
google.maps.event.removeListener(listener);
});
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById('directions-panel'));
}
function calcRoute() {
var start = document.getElementById('start').value;
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
travelMode: google.maps.DirectionsTravelMode.TRANSIT
};
directionsService.route(request, function (response, status) {
if (status == google.maps.DirectionsStatus.OK) {
directionsDisplay.setDirections(response);
}
});
}
google.maps.event.addDomListener(window, 'load', initialize);
答案 0 :(得分:1)
在创建时将标记保存在数组中,例如(在创建循环中添加):
marker = new google.maps.Marker({
...
});
mkArray.push(marker); // <------
然后:
var i, j=mkArray.length-1;
// Keeping the first and last one
for(i=1;i<j;i++) {
mk=mkArray[i];
mk.setMap(null);
}
答案 1 :(得分:0)
使用以下代码替换calcRoute()函数,然后在var waypts中添加您选择的位置。中途停留:false将自动删除其他标记。
function calcRoute()
{
var start = document.getElementById('start').value;
var waypts = [{Location:AddAnyLocationThatIsInYourRoute,stopover:false}];
var end = document.getElementById('end').value;
var request = {
origin: start,
destination: end,
waypoints:waypts,
travelMode: google.maps.DirectionsTravelMode.TRANSIT
};