我的谷歌地图不会显示每个标记

时间:2011-09-21 09:24:49

标签: javascript marker

大家,嘿嘿, 我需要用我们客户的标记制作谷歌地图 但是我还不擅长编程仍然在学校,并没有真正得到太多的编程:( 但是我的问题是我能够在地图上获得一些标记,但不是所有这些标记,它总是会显示不同的数量。我的工作地点不是基于纬度和经度。

这是我的脚本

    <!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">
  <head>
    <meta http-equiv="content-type" content="text/html; charset=utf-8"/>
    <title>Google Maps JavaScript API Example</title>
    <script src="http://maps.google.com/maps?file=api&v=2&key=ABQIAAAAjU0EJWnWPMv7oQ-jjS7dYxTPZYElJSBeBUeMSX5xXgq6lLjHthSAk20WnZ_iuuzhMt60X_ukms-AUg"
    type="text/javascript"></script>
    <script type="text/javascript">
    //<![CDATA[

        //load Google Map
        function load() {
          if (GBrowserIsCompatible()) {
                var map = new GMap2(document.getElementById("map"));
                var geocoder = new GClientGeocoder();
                map.addControl(new GLargeMapControl()); 
                map.addControl(new GMapTypeControl()); 

        //create randomnumber to prevent caching and retrieve xml file
        //data.xml vervangen door weg naar .xml bestand
        GDownloadUrl("data.xml", function(data, responseCode) {
        var xml = GXml.parse(data);

        //store markers in markers array
        var markers = xml.documentElement.getElementsByTagName("marker");

        //while loop ipv for loop
        var i = 0
        do
            {
            var address = markers[i].getAttribute("address");
            var html = markers[i].getAttribute("html");
            showAddress(map,geocoder,address,html);
            timer();
            }
        while (i <= 18);


            function timer()
        {
        i++
        setTimeout("timer()",1000);
        }

        //Create marker and set up event window
        function createMarker(point,html){
          var marker = new GMarker(point);
          GEvent.addListener(marker, "click", function() {
             marker.openInfoWindowHtml(html);
          });
          return marker;
        }

        //showAddress
        function showAddress(map,geocoder,address,html) {
          geocoder.getLatLng(
            address,
            function(point) {
              if (!point) {
                alert(address + " niet gevonden");
              } else {
                map.setCenter(point, 12);
                var marker = createMarker(point,html+'<br/><br/>'+address);
                map.addOverlay(marker);
              }
            }
          );
        }


          }
        ); //close GDownloadUrl
  } //close GBrowserIsCompatible
} //close load
   //]]>
    </script>
  </head>
  <body onload="load()" onunload="GUnload()">
    <div id="map" style="width: 1000px; height: 700px"></div>
  </body>
</html>

1 个答案:

答案 0 :(得分:1)

这可能是由于Google API限制了您可以进行的查询次数。如果你过快地询问地址的位置,那么在前几次请求之后它就会拒绝你。

我看到你试图用你的timer()函数解决这个问题,但是你编写它的方式实际上并没有延迟while循环(Javascript会很快设置计时器)继续循环的下一次迭代)。您需要做的是将调用带到由setTimeout调用的代码中的API,如下所示:

function placeMarker(i) {

    var address = markers[i].getAttribute("address"),
        html = markers[i].getAttribute("html");

    showAddress(map,geocoder,address,html);

    if (++i <= 18) {

        setTimeout(
            function() {
                placeMarker(i);
            },
            1000
        );

    }

}

placeMarker(0);