0

我无法将可点击的信息框添加到我的谷歌地图路线中。

我可以让它在初始化函数中工作,但是当我通过我的 xml 文件添加额外的标记时,我需要让它工作。请参阅下面的完整代码。

谢谢你的帮助..

var directionDisplay;
var directionsService = new google.maps.DirectionsService();
var stepDisplay;
var map;
var markersArray;
var markers = [];
var infowindow;




function initialize() {
var latlng = new google.maps.LatLng( <? echo $lat; ?> , <? echo $lon; ?> );
var rendererOptions = {
    draggable: true
};
directionsDisplay = new google.maps.DirectionsRenderer(rendererOptions);
var myOptions = {
    zoom: 14,
    center: latlng,
    mapTypeId: google.maps.MapTypeId.ROADMAP,
    mapTypeControl: false
};
map = new google.maps.Map(document.getElementById("map_canvas"), myOptions);
directionsDisplay.setMap(map);
directionsDisplay.setPanel(document.getElementById("directionsPanel"));
}

//ADD LIST
google.maps.event.addListener(marker, 'click', function() {
infowindow.open(map,marker);
});




function calcRoute() {
var travelMode = 'DRIVING';
var start = $("#routeStart").val();
var via = $("#routeVia2").val();
var end = $("#routeVia").val();
var waypoints = [];
if (via != '') {
    waypoints.push({
        location: via,
        stopover: true
    });
}
var request = {
    origin: start,
    destination: end,
    waypoints: waypoints,
    unitSystem: google.maps.UnitSystem.IMPERIAL,
    travelMode: google.maps.DirectionsTravelMode[travelMode]
};


directionsService.route(request, function (response, status) {
    if (status == google.maps.DirectionsStatus.OK) {            
       directionsDisplay.setDirections(response);
        var myRoute = response.routes[0];
        var txtDir = '';
        for (var i = 0; i < myRoute.legs[0].steps.length; i++) {
            txtDir += myRoute.legs[0].steps[i].path + "";
        }
        var strLAT = "" + txtDir;
        //---------------------------------------------------------     
        //SEND DATA TO URL
        var DATET = $("#DATET").val();
        var TIMET = $("#TIMET").val();
        var xmlHttp = getXMLHttp();
        xmlHttp.onreadystatechange = function () {
            if (xmlHttp.readyState == 4) {
                HandleResponse(xmlHttp.responseText);
            }
        }
        xmlHttp.open("POST", 'MYPHPFILE', true);
        xmlHttp.setRequestHeader("Content-type", "application/x-www-form-urlencoded");
        xmlHttp.send("LATLON=" + strLAT + "&getURL=" + end + "&TIMET=" + TIMET + "&DATET=" + DATET);
        //-------------------------------------------------------
        //RETURN DATA FOR PLACE MARKERS 
       var getURL = "MYXMLFILE" + end + ".xml";

       jQuery.get(getURL, function(data) {
       jQuery(data).find("marker").each(function() {
       var eachMarker = jQuery(this);
       var markerCoords = new google.maps.LatLng(
       parseFloat(eachMarker.find("Lat").text()),
       parseFloat(eachMarker.find("Lng").text())
       );
       var header  = eachMarker.find("title").text();
       var content = eachMarker.find("Content").text();
       var wxicon  = eachMarker.find("icon").text();
       //-------------------------------------------------------------------------- 
       //ADD INFO BOXES
       var contentString = "HELLO WORLD";
       var infowindow = new google.maps.InfoWindow({
       content: contentString,
       });         
       //--------------------------------------------------------------------------
        marker = new google.maps.Marker({
        position: markerCoords,
        icon: wxicon,
        map: map,
        animation: google.maps.Animation.DROP,
        title: header,
        });
        });
        });
        //--------------------------------------------------------------------------
    } else {
        //ERROR MESSAGES
        if (status == 'ZERO_RESULTS') {
            alert('No route could be found between the origin and destination.');
        } else if (status == 'INVALID_REQUEST') {
            alert('The DirectionsRequest provided was invalid.');
        } else {
            alert("There was an unknown error in your request. Requeststatus: nn" + status);
        }
    }
});
}
4

1 回答 1

0

已修复,我将标记更改为此,见下文。回答http://you.arenot.me/2010/06/29/google-maps-api-v3-0-multiple-markers-multiple-infowindows/

 //ADD INFO BOX
       var contentString = "HELLO WORLD";
       var infowindow = new google.maps.InfoWindow({
       content: htmls,
       });  
       //--------------------------------------------------------------------------
        marker = new google.maps.Marker({
        position: markerCoords,
        icon: wxicon,
        map: map,
        animation: google.maps.Animation.DROP,
        title: header,
        html: htmls,
        });
        var contentString = "HELLO WORLD";
        var infowindow = new google.maps.InfoWindow({
        content: htmls,
        }); 
        google.maps.event.addListener(marker, 'click', function () {
        infowindow.setContent(this.html);
        infowindow.open(map, this);
        });
        }); 
        });
于 2013-03-27T19:57:29.167 回答