Code:
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<!--
Example from Nokia Maps API Playground, for more information visit http://api.maps.nokia.com
-->
<html xmlns="http://www.w3.org/1999/xhtml">
<head>
<meta http-equiv="X-UA-Compatible" content="IE=7; IE=EmulateIE9"/>
<meta http-equiv="content-type" content="text/html; charset=UTF-8"/>
<title>Nokia Maps API Example: KML hierarchy tree</title>
<meta name="description" content="Using KML Manager we can modify the visualization of a loaded KML file"/>
<meta name="keywords" content="kmltree,KML"/>
<!-- For scaling content for mobile devices, setting the viewport to the width of the device-->
<meta name="viewport" content="initial-scale=1.0, maximum-scale=1.0, user-scalable=yes" />
<!-- Styling for example container (NoteContainer & Logger) -->
<link rel="stylesheet" type="text/css" href="http://api.maps.nokia.com/en/playground/exampleHelpers.css"/>
<!-- By default we add ?kml=auto&heatmap=auto to auto load KML & heatmap package remove params if package are not needed -->
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/2.2.1/jsl.js?with=all"></script>
<!-- JavaScript for example container (NoteContainer & Logger) -->
<script type="text/javascript" charset="UTF-8" src="http://api.maps.nokia.com/en/playground/exampleHelpers.js"></script>
<style type="text/css">
html {
overflow:hidden;
}
body {
margin: 0;
padding: 0;
overflow: hidden;
width: 100%;
height: 100%;
position: absolute;
}
#mapContainer {
width: 100%;
height: 100%;
left: 0;
top: 0;
position: absolute;
}
</style>
</head>
<body>
<div id="mapContainer"></div>
<script type="text/javascript" id="exampleJsSource">
/* Set authentication token and appid
* WARNING: this is a demo-only key
* please register on http://api.developer.nokia.com/
* and obtain your own developer's API key
*/
nokia.Settings.set( "appId", "_peU-uCkp-j8ovkzFGNU");
nokia.Settings.set( "authenticationToken", "gBoUkAMoxoqIWfxWA5DuMQ");
// Get the DOM node to which we will append the map
var mapContainer = document.getElementById("mapContainer"),
// We create a new instance of InfoBubbles bound to a variable so we can call it later on
infoBubbles = new nokia.maps.map.component.InfoBubbles(),
// Create a map inside the map container DOM node
map = new nokia.maps.map.Display(mapContainer, {
components: [
// we add the behavior component to allow panning / zooming of the map
new nokia.maps.map.component.Behavior(),
infoBubbles
],
zoomLevel: 2
}),
TOUCH = nokia.maps.dom.Page.browser.touch,
EventTarget = nokia.maps.dom.EventTarget;
/* Create a class KMLTree to encapsulate the functionality
* of the tree structure that appears on the right hand-side of th
* map.
* The constructor function takes a KML result set that is returned by the KmlManager
*/
var KMLTree = function (kmlDocument, kmlMapContainer) {
var that = this,
i,
labels,
labelsLength,
// Create container DIV element
kmlTreeContainerElt = document.createElement("div");
kmlTreeContainerElt.style.position = "absolute";
kmlTreeContainerElt.style.top = "0";
kmlTreeContainerElt.style.right = "0";
kmlTreeContainerElt.style.width = "260px";
kmlTreeContainerElt.style.height = "100%";
kmlTreeContainerElt.style.background = "white";
kmlTreeContainerElt.style.overflow = "auto";
kmlTreeContainerElt.style.paddingLeft = "5px";
kmlTreeContainerElt.style.borderLeft = "1px solid #666";
kmlTreeContainerElt.style.zIndex = "999";
document.body.appendChild(kmlTreeContainerElt);
// Create the labels for all the entries in the geometry object
kmlTreeContainerElt.appendChild( that._getAllLabels(kmlDocument) );
// The EventTarget function takes care of normalizing
// event behavior across browsers.
EventTarget(kmlTreeContainerElt);
kmlTreeContainerElt.addListener(TOUCH ? "tap" : "click", function (evt) {
var i,
bbox,
map = window.map,
obj = kmlMapContainer.objects.get(0),
target = evt.target,
tagName = target.tagName.toLowerCase(),
isCheckboxClicked = tagName == "input",
isNameClicked = tagName == "a",
indices,
checkboxState;
if (target && (isCheckboxClicked || isNameClicked)) {
indices = target.parentNode.indices;
for (i = 0, len = indices.length; i < len; i++) {
obj = obj.objects.get(indices[i]);
}
if (isNameClicked) { // name was clicked
if ((bbox = obj.getBoundingBox())) map.zoomTo(bbox);
} else { // checkbox was clicked
checkboxState = target.checked ? true : false;
obj.set("visibility", checkboxState);
}
}
}, false);
};
KMLTree.prototype = {
_append: function (parentNode, node) {
if ( parentNode && node) {
parentNode.appendChild(node);
}
},
_getAllLabels: function (kmlObject, index, parentNode) {
var that = this,
labels = [],
kml = nokia.maps.kml,
i = 0,
labelEntry,
features = kmlObject.features || [];
parentNode = parentNode || document.createElement("div");
index = index || [];
while (i < features.length) {
kmlObject = features[i];
labelEntry = that._getLabel(kmlObject, index.concat(i));
if ((kmlObject instanceof kml.Document) || (kmlObject instanceof kml.Folder)) {
that._append(parentNode, that._getAllLabels(kmlObject, index.concat(i), labelEntry));
}
that._append(parentNode, labelEntry);
i++;
}
return parentNode;
},
_getLabel: function (kmlObject, indices) {
var linkElt = document.createElement("a"),
containerElt = document.createElement("div"),
checkboxElt = document.createElement("input");
// Let's do not show kml objects which have node <open>0</open> in KML
if (kmlObject.open == "0") {
return;
}
checkboxElt.type = "checkbox";
checkboxElt.style.width = "auto";
checkboxElt.style.marginLeft = (4 + (indices.length - 1) * 10) + "px";
linkElt.innerHTML = linkElt.title = kmlObject.name ? kmlObject.name : "";
linkElt.style.cursor = "pointer";
containerElt.indices = indices;
containerElt.appendChild(checkboxElt);
// IE8 and lower can't set checked attribute before element is appended
checkboxElt.checked = kmlObject.visibility ? true : false;
containerElt.appendChild(linkElt);
return containerElt;
}
};
// We initialize KMLManager so parse KML files
var kmlManager = new nokia.maps.kml.Manager(),
// We define a callback function for parsing kml file,
// and then push the parsing result to map display
onParsed = function (kmlManager) {
var resultSet,
container,
boundingBox;
// KML file was successfully loaded
if (kmlManager.state == "finished") {
// KML file was successfully parsed
resultSet = new nokia.maps.kml.component.KMLResultSet(kmlManager.kmlDocument, map);
resultSet.addObserver("state", function (resultSet) {
if (resultSet.state == "finished") {
// KML objects tree was successfully converted into map objects
new KMLTree(kmlManager.kmlDocument, resultSet.container);
boundingBox = container.getBoundingBox();
// Here we check whether we have valid bounding box or no.
// In case if KML document does not contain any supported displayable element, bounding box will be a null,
// therefore it will not be possible to zoom to the not existing object.
if (boundingBox) {
// Switch the viewport of the map to show all KML map objects within the container
map.zoomTo(boundingBox);
}
}
});
// Manually push map objects/container to map display
map.objects.add(container = resultSet.create());
}
}
// We add an observer to kml manager
kmlManager.addObserver("state", onParsed);
/// WORKS IN IE ONLY
kmlManager.parseKML("http://henrysinn.com/gmm/myki.kml");
</script>
</body>
</html>