Display KML Data
Parse a KML file and display the data on a map.
HTML
<script src="https://heremaps.github.io/maps-api-for-javascript-examples/test-credentials.js"></script>
<!DOCTYPE html>
<html>
<head>
<meta name="viewport" content="width=device-width, initial-scale=1.0, maximum-scale=1.0, user-scalable=yes">
<meta http-equiv="Content-type" content="text/html;charset=UTF-8">
<meta name="description" content="Parse a KML file and display the data on a map">
<title>Display KML Data</title>
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.2/mapsjs-ui.css" />
<script type="text/javascript" src="https://js.api.here.com/v3/3.2/mapsjs-core.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.2/mapsjs-service.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.2/mapsjs-ui.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.2/mapsjs-mapevents.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.2/mapsjs-data.js"></script>
<link rel="stylesheet" type="text/css" href="../template.css" />
<link rel="stylesheet" type="text/css" href="demo.css" />
<script type="text/javascript" src='https://heremaps.github.io/maps-api-for-javascript-examples/test-credentials.js'></script>
<style type="text/css">
.log {
position: absolute;
top: 5px;
left: 5px;
height: 150px;
width: 250px;
overflow: scroll;
background: white;
margin: 0;
padding: 0;
list-style: none;
font-size: 12px;
}
.log-entry {
padding: 5px;
border-bottom: 1px solid #d0d9e9;
}
.log-entry:nth-child(odd) {
background-color: #e1e7f1;
}
</style>
</head>
<body>
<h1>Display KML Data</h1>
<p>This example shows a map of the <b>United States</b> from the <a href="https://www.census.gov" target="_blank">U.S. Census Bureau</a>, highlighting the borders of...
CSS
#map {
width: 95%;
height: 450px;
background: grey;
}
#panel {
width: 100%;
height: 400px;
}
JavaScript
/**
* Renders specified a KML file on the map
*
* Note that the maps data module https://js.api.here.com/v3/3.2/mapsjs-data.js
* must be loaded for KML parsing to occur
*
* @param {H.Map} map A HERE Map instance within the application
*/
function renderKML(map) {
// Create a reader object passing in the URL of our KML file
reader = new H.data.kml.Reader(
"https://heremaps.github.io/maps-api-for-javascript-examples/display-kml-on-map/data/us-states.kml"
);
reader.addEventListener("statechange", function (evt) {
if (evt.state === H.data.AbstractReader.State.READY) {
// Get KML layer from the reader object and add it to the map
map.addLayer(reader.getLayer());
reader
.getLayer()
.getProvider()
.addEventListener("tap", (evt) => {
logEvent(evt.target.getData().name);
});
}
if (evt.state === H.data.AbstractReader.State.ERROR) {
logEvent("KML parsing error");
}
});
// Parse the document
reader.parse();
}
/**
* Boilerplate map initialization code starts below:
*/
// Step 1: initialize communication with the platform
// In your own code, replace variable window.apikey with your own apikey
var platform = new H.service.Platform({
apikey: window.apikey
});
var defaultLayers = platform.createDefaultLayers();
// Step 2: initialize a map
var map = new H.Map(
document.getElementById("map"),
defaultLayers.vector.normal.map,
{
zoom: 2.5,
center: { lat: 48.30432303555956, lng: -104.94466241321628 },
pixelRatio: window.devicePixelRatio || 1,
}
);
// add a resize listener to make sure that the map occupies the whole container
window.addEventListener("resize", () => map.getViewPort().resize());
// Step 3: make the map interactive
// MapEvents enables the event system
// Behavior implements default interactions for pan/zoom (also on mobile touch environments)
var behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map));
// Step 4: create...