从javascript读取kml中的特定属性

时间:2014-08-15 16:25:37

标签: javascript kml leaflet

我有mapquest API的地图,我也使用Leaflet显示邮政编码,L.divIcon位于多边形的中心,显示Zip代码。一切都很好,除了我必须手动输入多边形的中心作为在javascript中显示divIcon的地方。

我想知道的是,是否有办法从javascript读取kml以获得多边形的中心。我可以使用Qgis计算中心并将其保存到kml谎言:

    ........
    <Placemark>

    <name>77029</name>

    <ExtendedData><SchemaData schemaUrl="#center_jacinto">

    <SimpleData name="Descriptio">Jacinto City</SimpleData>
    </SchemaData></ExtendedData>
    //This is the data I want to read from javascript if I can->
    <Point><coordinates>-95.262889587410186,29.761518060337306</coordinates> 
    </Point>
    .....

kml将绘制一个我不想要的标记,我只想要lat和lon,然后我将绘制我的图标..

编辑:我添加了这个但是什么都没有,javascript和html非常新,所以我确定顺序错了

     <html>

     <head>
     <title>Leaflet</title>
     <link rel="stylesheet" href="leaflet.css" />
     <link rel="stylesheet" href="leaflet.label.css" />


     <script src="leaflet.js"></script>
    <script src="leaflet.label.js"></script>

    <script src="KML.js"></script>
    <script src="http://www.mapquestapi.com/sdk/leaflet/v1.0/mq-map.js?key=key">
   </script>





   </head>

   </body >
   <div style="color:black ;width:100%; height:100%" id="map"></div>
    <script type='text/javascript'>




   function myfunction()
   {
   alert('ok');
   if (window.XMLHttpRequest)
   {// code for IE7+, Firefox, Chrome, Opera, Safari
   xmlhttp=new XMLHttpRequest();
   }
   else
   {// code for IE6, IE5
   xmlhttp=new ActiveXObject("Microsoft.XMLHTTP");
   }

   xmlhttp.open("GET","center_jacinto.kml",false);
   xmlhttp.send();
   xmlDoc=xmlhttp.responseXML;
   parser   = new DOMParser(); // new Parser
   xmlDoc = parser.parseFromString(xmlDoc,"text/xml"); // Parse string
   txt=xmlDoc.getElementsByTagName("Point")[0].childNodes[0].nodeValue;
   console.log(txt);
   }



   window.load = myfunction;




    var mapLayer = MQ.mapLayer();


    var map = new L.Map('map', {
    center: new L.LatLng(29.7630556,-95.3630556),
    zoom: 10
   });
   var layer = L.tileLayer('http://otile{s}.mqcdn.com/tiles/1.0.0/sat/{z}/{x}/{y}.jpeg', {

       attribution: 'xxxx',
       subdomains: '1234',
       }).addTo(map);



       var track = new L.KML("center_jacinto.kml", {async: true});
        track.on("loaded", function(e) { map.fitBounds(e.target.getBounds()); });



    map.addControl(new L.Control.Layers({'map': mapLayer.addTo(map)}, 
        {'jacinto':track

        }));

    console.log(map.getZoom());
    map.addLayer(track);


    map.addLayer(layer);






   </script>
   </body>


   </html>

2 个答案:

答案 0 :(得分:0)

shramov KML plugin不支持读取SimpleData属性。您可能希望使用leaflet-omnivore,因为它确实如此。

答案 1 :(得分:0)

似乎有一个拉动请求允许Sharmov KML插件支持ExtendedData等等。

https://github.com/shramov/leaflet-plugins/pull/165

替代选项包括: