KML linkInfo ex. name snippet

1495
9
10-06-2011 03:00 PM
NahmLee
New Contributor
Q. How to get "name" or "snippet" from KML file?
ex. ESRI ex. name should be WY.kml.

http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/kmllayer.htm#featureInfos

<Object> linkInfo
A link info object with properties that describe the network link. The object has the following properties:

{
  "id": <Number>,
  "name": <String>,
  "description": <String>,
  "snippet": <String>,
  "visibility": <Number>
  "refreshMode": <String>,
  "refreshInterval": <Number>,
  "viewRefreshMode": <String>,
  "viewRefreshTime": <Number>,
  "viewBoundScale": <Number>,
  "viewFormat": <String>,
  "httpQuery": <String>
}

ESRI ex. http://help.arcgis.com/en/webapi/javascript/arcgis/help/jssamples_start.htm?KMLLayer

Thanks,
0 Kudos
9 Replies
derekswingley1
Frequent Contributor
Once your KMLLayer loads, call getLayers() on it. This will return the feature layers that make up the KMLLayer (one for each type of geometry present for a possible total of three- points, lines, polygons). Once you have you feature layer, loop through the graphics and look at the attributes.name property for each graphic.

If you run this JS on the simple KMLLayer sample:
map.getLayer(map.graphicsLayerIds[0]).graphics[0].attributes.name


It will print "Wyoming(WY)"
0 Kudos
NahmLee
New Contributor
0 Kudos
derekswingley1
Frequent Contributor
My previous comment still applies...give it a try and let us know if you have any issues.
0 Kudos
NahmLee
New Contributor
I need to study more because didn't get it yet.
0 Kudos
NahmLee
New Contributor
It works in a strange way but I think it was an accident...

https://s3.amazonaws.com/Nahm/KML/KML4.html

function NameKML(){
var folders = kml.folders;
dojo.forEach(folders,function(folder){
  alert(kml.folders[0].name);
  alert(kml.folders[1].name);
});

}
0 Kudos
NahmLee
New Contributor
KML file tags display if button is clicked but I can't write them in a innerHTML when KML is loaded in init() function. Why???


<!doctype html>
<html lang="en">
<head>
<title>KML name</title>
<meta charset="utf-8">
<meta http-equiv="X-UA-Compatible" content="IE=7,IE=9" />
<meta name="viewport" content="initial-scale=1, maximum-scale=1,user-scalable=no"/>
<title></title>
<link rel="stylesheet" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.5/js/dojo/dijit/themes/tundra/tundra.css">
<link rel="stylesheet" type="text/css" href="http://serverapi.arcgisonline.com/jsapi/arcgis/2.5/js/esri/dijit/css/Popup.css"/>

<style>
html, body { height: 100%; width: 100%; margin: 0; padding: 0; }
#map { margin: 0; padding: 0; }
#meta {
  position: absolute;
  left: 60px;
  Top: 20px;
  width: 300px;
  height: 300px;
  z-index: 40;
  background: #fff;
  color: #777;
  padding: 5px;
  border: 5px solid #777;
  -webkit-border-radius: 5px;
  -moz-border-radius: 5px;
  border-radius: 5px;
  font-family: arial;
  font-size: 0.9em;
}
#meta h3 {
  color: #000;
  font-size: 1.1em;
  padding: 0px;
  margin: 0px;
  display: inline-block;
}
#loading {
  float: right;
}
</style>
<script>var dojoConfig = { parseOnLoad: true };</script>
<script src="http://serverapi.arcgisonline.com/jsapi/arcgis/?v=2.5"></script>
<script>
dojo.require("dijit.layout.BorderContainer");
dojo.require("dijit.layout.ContentPane");
dojo.require("esri.map");
dojo.require("esri.layers.KMLLayer");

var map, resizeTimer;
var kmlUrl, kml;
function init() {
var initExtent = new esri.geometry.Extent({"xmin":-15042310.65,"ymin":1599979.71,"xmax":-6236764.99,"ymax":7470343.49,"spatialReference":{"wkid":102100}});
map = new esri.Map("map",{ extent: initExtent });
var basemap = new esri.layers.ArcGISTiledMapServiceLayer("http://server.arcgisonline.com/ArcGIS/rest/services/World_Topo_Map/MapServer");
map.addLayer(basemap);

kmlUrl = 'http://www.hpc.ncep.noaa.gov/kml/qpf/QPF6hr_f00-f06_latest.kml';
kml = new esri.layers.KMLLayer(kmlUrl);
map.addLayer(kml);
var folders = kml.folders;
//it doen't work????
document.all.here.innerHTML=kml.folders[1].name+" : <br />"+kml.folders[1].snippet;
}

function NameKML(){
var folders = kml.folders;
alert("kml name : " +kml.folders[1].name);
alert("kml snippet : "+kml.folders[1].snippet);
alert("kml description : "+kml.folders[1].description);
document.all.meta.innerHTML=kml.folders[1].description;
}

dojo.ready(init);
</script>
</head>

<body class="tundra">
<div data-dojo-type="dijit.layout.BorderContainer" data-dojo-props="design:'headline',gutters:false" style="width: 100%; height: 100%; margin: 0;">
  <div id="map" data-dojo-type="dijit.layout.ContentPane" data-dojo-props="region:'center'">
   <div id="meta">
    <h3>Display KML Using a <a href="http://help.arcgis.com/en/webapi/javascript/arcgis/help/jsapi/kmllayer.htm">KMLLayer</a></h3>
    <br />
    <input type="button" value="Name KML" onclick="NameKML();" /><br />
    <div id="here">
    </div>
   </div>
  </div>
</div>
</body>
</html>
0 Kudos
derekswingley1
Frequent Contributor
JavaScript executes asynchronously so the code to create your KMLLayer starts but isn't finished when you're trying to access the folders property. To get your folder info, connect to the KMLLayer's onLoad event:
kmlUrl = 'http://www.hpc.ncep.noaa.gov/kml/qpf/QPF6hr_f00-f06_latest.kml';
kml = new esri.layers.KMLLayer(kmlUrl);
map.addLayer(kml);
dojo.connect(kml, 'onLoad', function(kl) {
  alert('kml loaded. folder[1].name is: ' + kl.folders[1].name);
});


This pattern applies to all layer types in the JS API.
0 Kudos
NahmLee
New Contributor
Thank you very much!!!
It works fine now.

https://s3.amazonaws.com/Nahm/KML/KML_Name_Final.html

Nahm
0 Kudos
derekswingley1
Frequent Contributor
Glad you got it working!
0 Kudos