I'm new to making Google Maps, the markers wont show up, can you see what's wrong?
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="content-type" content="text/html; charset=UTF-8" />
<title>Google Maps Multiple Markers</title>
<script src="http://maps.google.com/maps/api/js?sensor=false"
type="text/javascript"></script>
</head>
<body>
<div id="map" style="width: 500px; height: 400px;"></div>
<script type="text/javascript">
var map = new google.maps.Map(document.getElementById('map'), {
zoom: 10,
center: new google.maps.LatLng(-33.92, 151.25),
mapTypeId: google.maps.MapTypeId.ROADMAP
});
var xmlDoc;
if (window.XMLHttpRequest)
{
xmlDoc=new window.XMLHttpRequest();
xmlDoc.open("GET","locations.php",false);
xmlDoc.send("");
xmlDoc=xmlDoc.responseXML;
}
// IE 5 and IE 6
else if (ActiveXObject("Microsoft.XM开发者_开发百科LDOM"))
{
xmlDoc=new ActiveXObject("Microsoft.XMLDOM");
xmlDoc.async=false;
xmlDoc.load("locations.php");
}
var pins = xmlDoc.getElementsByTagName("pin");
for (i=0;i<pins.length;i++)
{
var point = new GLatLng( pins[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue,
pins[i].getElementsByTagName("lon")[0].childNodes[0].nodeValue);
var colord;
var curgender = pins[i].getElementsByTagName("gender")[0].childNodes[0].nodeValue;
if(curgender == "Male")
{colord = blueOpt;}else if(curgender=="Female"){colord = pinkOpt;}else{colord = purpleOpt;}
var marker = new GMarker(point, colord);
var mess = pins[i].getElementsByTagName("message")[0].childNodes[0].nodeValue;
createMarker(marker,mess);
map.addOverlay(marker);
}
function createMarker(marker, mess) {
GEvent.addListener(marker, "click", function() {
marker.openInfoWindowHtml(mess);
});
}
</script>
</body>
</html>
XML
<xml>
<pin>
<lat>-33.890542</lat>
<lon>151.274856</lon>
<gender>Male</gender>
<message>This is message 1</message>
</pin>
<pin>
<lat>-33.923036</lat>
<lon>151.259052</lon>
<gender>Female</gender>
<message>This is message 2</message>
</pin>
</xml>
You have a mixture of v2 and v3 code. The map is created with version 3 and the Marker is using v2 code.
I assume you want to use v3 since v2 is depreciated.
try this:
var infoWindow = new google.maps.InfoWindow({});
for (i=0;i<pins.length;i++)
{
var point = new google.maps.LatLng(
pins[i].getElementsByTagName("lat")[0].childNodes[0].nodeValue,
pins[i].getElementsByTagName("lon")[0].childNodes[0].nodeValue
);
var colord;
var curgender = pins[i].getElementsByTagName("gender")[0].childNodes[0].nodeValue;
if(curgender == "Male"){
colord = blueOpt; //I assume these are different marker icons?
}else if(curgender=="Female"){
colord = pinkOpt;
}else{
colord = purpleOpt;
}
var marker = new google.maps.Marker({
position: point
icon: colord
});
var mess = pins[i].getElementsByTagName("message")[0].childNodes[0].nodeValue;
createMarker(marker,mess);
marker.setMap(map);
}
function createMarker(marker, mess) {
google.maps.event.addListener(marker, "click", function (event) {
infoWindow.setContent(mess);
infoWindow.open(map, this);
});
}
精彩评论