简体   繁体   English

从javascript中读取kml中的特定属性

[英]read a specific attribute in kml from javascript

I have a map from mapquest API, and I'm also using Leaflet to display the zip codes, with a L.divIcon at the center of the polygon displaying the Zip code. 我有一个来自mapquest API的地图,并且我还使用Leaflet来显示邮政编码,在多边形中心的L.divIcon显示了Zip代码。 Everything is working great except that I have to manually input the center of the polygon as a place to display the divIcon in javascript. 一切工作正常,除了我必须手动输入多边形的中心作为在javascript中显示divIcon的地方。

What I would like to know is if there is a way to read the kml from javascript to get the center of the polygon. 我想知道的是,是否有一种方法可以从javascript中读取kml以获取多边形的中心。 I can use Qgis to calculate the center and save it to a kml lie this: 我可以使用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>
    .....

The kml will draw a marker which I don't want, I just want the lat and lon, which I will then draw my icon.. kml将绘制一个我不想要的标记,我只想要lat和lon,然后绘制我的图标。

edit:I added this but getting nothing, very new to javascript and html so I'm sure the order is wrong 编辑:我添加了这个,但是什么也没得到,对于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>

The shramov KML plugin doesn't support reading SimpleData attributes. shramov KML插件不支持读取SimpleData属性。 You might want to use leaflet-omnivore instead, since it does. 您可能想要改用letlet-omnivore ,因为确实如此。

It seems there is a pull request to allow Sharmov KML plugin to support ExtendedData and more. 似乎有一个请求请求,以允许Sharmov KML插件支持ExtendedData等。

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

Alternative options are: 替代选项是:

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM