使用geokml3在Google地图上加载KML图层

时间:2014-05-14 10:28:12

标签: javascript google-maps geoxml3

我想使用Geoxml3在谷歌地图上加载kml文件,谷歌地图已加载但图层未显示,我不知道我的代码有什么问题,有人可以帮忙吗?

<script type="text/javascript">
var mapInstance;
var parser;

function doAlert() {
        alert("Parsed triggered!");
}

function initialize() {
        console.log("in init");
        var latlng = new google.maps.LatLng(41.1188827, 1.24449090);
        var mapOptions = {
                zoom: 14,
                center: latlng,
                mapTypeId: google.maps.MapTypeId.ROADMAP,
                mapTypeControlOptions: {
                style: google.maps.MapTypeControlStyle.DEFAULT
                }
        };
        mapInstance = new google.maps.Map(document.getElementById("map"), mapOptions);
        parser = new geoXML3.parser({
                map: mapInstance,
                zoom: false,
                processStyles: true,
                singleInfoWindow: true
                }
        );
        google.maps.event.addListener(parser, 'parsed', doAlert);       
        parser.parse('postes.kml');
}               
</script>
</head>
<body onload="initialize()">
        <div id="map" style="width: 100%; height: 100%"></div>
</body>
</html>

1 个答案:

答案 0 :(得分:0)

你不创建只解析kml的Marker,但是不要将它设置为map.This是来自https://code.google.com/p/geoxml3/wiki/Usage

的代码示例
<script type="text/javascript">
    var myMap = new google.maps.Map(...);

    var myParser = new geoXML3.parser({
      map: myMap,
      processStyles: true,
      createMarker: addMyMarker,
      createOverlay: addMyOverlay
    });
    myParser.parse(['my_geodata1.kml', 'my_geodata2.kml']);

    function addMyMarker(placemark) {
      // Marker handling code goes here
      if (someCondition) {
        myParser.createMarker(placemark);
      }
    };

    function addMyOverlay(groundOverlay) {
      // Overlay handling code goes here
      if (someCondition) {
        myParser.createOverlay(groundOverlay);
      }
    };
  </script>