簡體   English   中英

從javascript中讀取kml中的特定屬性

[英]read a specific attribute in kml from javascript

我有一個來自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>

shramov KML插件不支持讀取SimpleData屬性。 您可能想要改用letlet-omnivore ,因為確實如此。

似乎有一個請求請求,以允許Sharmov KML插件支持ExtendedData等。

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

替代選項是:

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM