我正在尝试将 addListeners 分配给有多个标记但不起作用的单个标记。发生的事情是所有的窗口都同时打开,即使我没有点击。当我点击时,什么也没有发生。怎么了?
这是我的代码:
<script type="text/javascript" src="http://maps.googleapis.com/maps/api/js?key=AIzaSyDUE08r9kD1p5QsqOzmI6_EcoUNCJntf5I&sensor=false"></script>
<script type="text/javascript">
//start the map. code from google maps tutorial.
function initialize() {
var mapOptions = {
center: new google.maps.LatLng(37.7750, -122.4183),
zoom: 11,
mapTypeId: google.maps.MapTypeId.ROADMAP
};
//return a map for later use
return new google.maps.Map(document.getElementById("map_canvas"),
mapOptions);
}
function request() {
$.ajax ({
type: "GET",
dataType: "json",
url: "stations.php",
data: {
route: $("#route").val()
},
success: function(data) {
var routePathCoords = [];
var pathColor = data.color;
$.each(data.stations, function(i,j) {
routePathCoords.push(new google.maps.LatLng(data.lat[i], data.longit[i]));
});
//create new map
var map = initialize();
//generating the polyline
var routePath = new google.maps.Polyline({
path: routePathCoords,
strokeColor: pathColor,
strokeOpacity: 1.0,
strokeWeight: 5
});
routePath.setMap(map);
//creating each marker, display on screen
$.each(routePathCoords, function(i,c) {
var marker = new google.maps.Marker({
//obtaining from coordinates array
position: c,
map: map,
//obtaining from stations array
title: data.stations[i]
});
var name = marker.title;
//listen for click, then activate window
google.maps.event.addListener(marker, 'click', stnWindow(name, marker, map));
});
},
error:function (xhr, ajaxOptions, thrownError){
console.log(ajaxOptions);
alert(xhr.status);
alert(thrownError);
}
});
return false;
}
function stnWindow(name, marker, map) {
$.ajax({
type: "GET",
dataType: "json",
url: "window.php",
data: {
name: name
},
success: function(data) {
//initializing variables for window+content
var station = data.fullname;
var infoWindow = new google.maps.InfoWindow;
//adding station title
var content = "<h1>" + station + "</h1>";
//get names and times, put into var content
$.each(data.destns, function(i,n) {
content = content + "<h2> Destination: </h2>" + n;
content = content + "<h3> Arrival: </h3>" + data.arrivals[i] + "minutes" ;
});
infoWindow.setContent(content);
infoWindow.open(map, marker);
}
})
}
</script>
<script type="text/javascript">
</script>