Skip to content

Add polygon hovering example #170

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Open
wants to merge 6 commits into
base: master
Choose a base branch
from
Open
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
6 changes: 6 additions & 0 deletions meta.json
Original file line number Diff line number Diff line change
Expand Up @@ -256,6 +256,12 @@
"title": "Draggable geoshapes",
"shortDescription": "Display geoshapes which can be moved around the map",
"published": true
},
{
"uid": "polyons-hovering",
"title": "Hovering Polygons",
"shortDescription": "Hover over polygons to see them highlighted",
"published": true
}
]
},
Expand Down
38 changes: 38 additions & 0 deletions polygon-hovering/data/germany-states.geojson

Large diffs are not rendered by default.

11 changes: 11 additions & 0 deletions polygon-hovering/demo.css
Original file line number Diff line number Diff line change
@@ -0,0 +1,11 @@

#map {
width: 95%;
height: 450px;
background: grey;
}

#panel {
width: 100%;
height: 400px;
}
12 changes: 12 additions & 0 deletions polygon-hovering/demo.details
Original file line number Diff line number Diff line change
@@ -0,0 +1,12 @@
---
name: Hoverable Polygons
description: Display geo polygon with hover effects on a map
resources:
- https://heremaps.github.io/maps-api-for-javascript-examples/test-credentials.js
normalize_css: no
dtd: html 5
wrap: d
panel_html: 0
panel_js: 0
panel_css: 0
...
25 changes: 25 additions & 0 deletions polygon-hovering/demo.html
Original file line number Diff line number Diff line change
@@ -0,0 +1,25 @@
<!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 GeoJSON file and display the data on a map with hover effects">
<title>Polygon Hovering</title>
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-core.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-service.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-ui.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/mapsjs-mapevents.js"></script>
<script type="text/javascript" src="https://js.api.here.com/v3/3.1/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='../test-credentials.js'></script>
</head>
<body>
<h1>Polygon Hovering</h1>
<p> This example shows how to display objects from a GeoJSON file on the map and apply hover effects to polygons.
Hover over the polygons to see them highlighted.</p>
<div id="map"></div>
<script type="text/javascript" src='demo.js'></script>
</body>
</html>
77 changes: 77 additions & 0 deletions polygon-hovering/demo.js
Original file line number Diff line number Diff line change
@@ -0,0 +1,77 @@
/**
* Adds hoverable polygons to a HERE map using geoJSON data.
*
* @param {H.Map} map - A HERE Map instance.
*/
function addHoverablePolygons(map) {
const selectedStyle = new H.map.SpatialStyle({
lineWidth: 2,
strokeColor: 'red',
fillColor: 'green'
});

const defaultStyle = new H.map.SpatialStyle({
lineWidth: 1,
strokeColor: 'rgba(0,85,170,.6)',
fillColor: 'rgba(0,85,170,.4)'
});

// Load and parse the GeoJSON data
const reader = new H.data.geojson.Reader('data/germany-states.geojson', {
style: (feature) => {
// Setting Volatile to true, to enable style changes at runtime
feature.setVolatility(true);
feature.setStyle(defaultStyle);
},
disableLegacyMode: true
});

reader.parse();
const layer = reader.getLayer();
map.addLayer(layer);

let activeObject = null;

// Pointer move listener for hover styling
layer.getProvider().addEventListener('pointermove', (e) => {
const target = e.target;
if (target instanceof H.map.Object && target !== activeObject) {
if (activeObject) {
activeObject.setStyle(defaultStyle);
}
target.setStyle(selectedStyle);
activeObject = target;
}
});
}


/**
* Boilerplate map initialization code starts below:
*/
// Step 1: initialize communication with the platform
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: 5,
center: {lat: 50.89317884049538, lng: 9.163701875411466},
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));

// Create the default UI components
var ui = H.ui.UI.createDefault(map, defaultLayers);

// Step 4: Add geo polygon with hovering styles
addHoverablePolygons(map);
Empty file added polygon-hovering/styles.css
Empty file.