是否可以在Google Map中动态地将信息窗口附加到每个航路点?

时间:2019-05-20 08:19:21

标签: javascript jquery google-maps

我正在尝试将信息窗口附加到我的Google Map中的路标。问题是航路点本身从未声明过。它总是动态添加到地图中。我该如何将addListener()添加到从未声明的标记(航路点)中?

function initMap(){
    var directionsService = new google.maps.DirectionsService;
    var directionsDisplay = new google.maps.DirectionsRenderer;
    var map = new google.maps.Map(document.getElementById('map'),{
        zoom: 4,
        center: {lat: -2.548926, lng: 118.0148634}
    });
    directionsDisplay.setMap(map);
    $("body").on("click", "#geser", function(){

    var id = $(this).attr('name');
    $.post("backend/jadwal/gmap_proses.php",
    {
        idJ: id
    },
    function(data){
        //returned data.rute exmaple : "BANYUWANGI - JEMBER - SURABAYA - SUMENEP (PATAS)"
        var arrIdJ=data.idJadwal.split(" ");
        arrIdJ.pop();
        var arrRute=data.rute.split(" - ");
        var n = arrRute.length;
        arrRute[n-1] = arrRute[n-1].replace("(PATAS)", "");
        //the String (PATAS) is not important here i don't need it
        alert("idJadwal = "+arrIdJ+
        "\nRute = "+arrRute);
        var waypts=[];
        for (var i = 1; i < arrRute.length-1; i++){
            waypts.push({
                location: arrRute[i],
                stopover: true
            });
        }
        calculateAndDisplayRoute(directionsService, directionsDisplay, waypts, arrRute[0], arrRute[n-1]);
    }, "json");

});

}

function calculateAndDisplayRoute(directionsService, directionsDisplay,waypts, origin, destination){
    directionsService.route({
      origin: origin,
      destination: destination,
      waypoints: waypts,
      optimizeWaypoints: true,
      travelMode: 'DRIVING'
    }, function(response, status) {
      if (status === 'OK') {
        directionsDisplay.setDirections(response);
        var route = response.routes[0];
      } else {
        window.alert('Directions request failed due to ' + status);
      }
    });
}

0 个答案:

没有答案