1

如何在计算路线后删除 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);
4

2 回答 2

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);
}
于 2013-04-30T02:26:57.190 回答
0

用以下代码替换您的 calcRoute() 函数,然后在 var waypts 中添加您选择的位置。stopover: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
};
于 2014-05-06T12:15:48.367 回答