我有多个不同颜色标记的地图和一些信息窗口。一切正常,除非我点击一些标记(例如我有5个标记),它会打开光标,比方说,第4个标记,并始终从该标记显示信息窗口。因此它始终显示相同的信息窗口。
这是一个小的html文件,所以如果有人知道解决方案,我将不胜感激。
我也尝试使用google.maps.event.addDomListener
因为我认为存在问题,但它是相同的行为。
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Strict//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-strict.dtd">
<html xmlns="http://www.w3.org/1999/xhtml" xmlns:v="urn:schemas-microsoft-com:vml">
<head>
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Google Maps Javascript API v3 Example: Loading the data from an XML</title>
<script type="text/javascript" src="http://maps.google.com/maps/api/js?sensor=false"></script>
<script type="text/javascript" src="downloadxml.js"></script>
<script type="text/javascript">
//<![CDATA[
// global "map" variable
var map = null;
function initialize() {
var myOptions = {
zoom: 8,
center: new google.maps.LatLng(43.907787,-79.359741),
mapTypeControl: true,
mapTypeControlOptions: {style: google.maps.MapTypeControlStyle.DROPDOWN_MENU},
navigationControl: true,
mapTypeId: google.maps.MapTypeId.ROADMAP
}
map = new google.maps.Map(document.getElementById("map_canvas"),
myOptions);
google.maps.event.addListener(map, 'click', function() {
infowindow.close();
});
// Read the data from example.xml
downloadUrl("example7.xml", function(doc) {
var xmlDoc = xmlParse(doc);
var markers = xmlDoc.documentElement.getElementsByTagName("marker");
for (var i = 0; i < markers.length; i++) {
// obtain the attribues of each marker
var lat = parseFloat(markers[i].getAttribute("lat"));
var lng = parseFloat(markers[i].getAttribute("lng"));
var point = new google.maps.LatLng(lat,lng);
var html = markers[i].getAttribute("html");
var severity = parseFloat(markers[i].getAttribute("severity"));
var color= "66FF33";
if (severity == 0) color = "66FF33";
else if (severity == 1) color = "990099";
else if (severity == 2) color = "00CCFF";
var pinImage = new google.maps.MarkerImage("http://www.googlemapsmarkers.com/v1/" + color,
new google.maps.Size(21, 34),
new google.maps.Point(0,0),
new google.maps.Point(10, 34));
var marker = new google.maps.Marker({
position: point,
map: map,
icon: pinImage,
});
google.maps.event.addListener(marker, 'click', function() {
var alarmanchor1='<span class="url"><a href="' + html;
var alarmanchor2='" title="www" target="_blank">Event List</a></span>';
var contentString=alarmanchor1+alarmanchor2;
infowindow.setContent(contentString);
infowindow.open(map,marker);
});
}
});
}
var infowindow = new google.maps.InfoWindow(
{
size: new google.maps.Size(150,50)
});
//]]>
</script>
</head>
<body style="margin:0px; padding:0px;" onload="initialize()">
<table border="1">
<tr>
<td>
<div id="map_canvas" style="width: 1050px; height: 1200px"></div>
</td>
</tr>
</table>
<script src="urchin.js" type="text/javascript">
</script>
</body>
</html>
我的XML是:
<markers>
<marker lat="43.65654" lng="-79.90138" html="http://www.google.com" label="Marker One" severity="0" />
<marker lat="43.91892" lng="-78.89231" html="http://www.yahoo.com" label="Marker Two" severity="1" />
<marker lat="43.82589" lng="-79.10040" html="http://www.sport.com" label="Marker Three" severity="2" />
<marker lat="43.72589" lng="-79.60040" html="http://www.stackoverflow.com" label="Marker Fourth" severity="3" />
<marker lat="43.52589" lng="-79.70040" html="http://www.stackoverflow.com" label="Marker Five" severity="4" />
</markers>
答案 0 :(得分:1)
只需将代码更改为此代码,即可为每个元素获取正确的网址。
<a href="' + html;
到
<a href="' +markers[i].getAttribute("html");
所以问题的完整代码是:
google.maps.event.addListener(marker, 'click', (function(marker, i) { return function(){ var alarmanchor1='<span class="url"><a href="' +markers[i].getAttribute("html"); var alarmanchor2='" title="www" target="_blank">Event List</a></span>'; var contentString=alarmanchor1+alarmanchor2; infowindow.setContent(contentString); infowindow.open(map, marker); } }) (marker, i));