标记未显示在Google Map中

时间:2011-09-07 21:34:52

标签: javascript html xml google-maps

我是制作谷歌地图的新手,标记不会出现,你能看出什么是错的吗?

<!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.XMLDOM"))
        {
            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>

1 个答案:

答案 0 :(得分:0)

您混合使用v2和v3代码。地图使用版本3创建,标记使用v2代码。

我假设您要使用v3,因为v2已弃用。

试试这个:

 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);               
            }); 
        }
相关问题