简体   繁体   中英

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. 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.

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. I can use Qgis to calculate the center and save it to a kml lie this:

    ........
    <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..

edit:I added this but getting nothing, very new to javascript and html so I'm sure the order is wrong

     <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. You might want to use leaflet-omnivore instead, since it does.

It seems there is a pull request to allow Sharmov KML plugin to support ExtendedData and more.

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

Alternative options are:

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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