Polygon on the Map

Display a map highlighting a region or area

HTML

<link rel="stylesheet" href="https://js.api.here.com/v3/3.0/mapsjs-ui.css">
<!-- SCRIPTS -->
<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-clustering.js"></script>

<!-- BODY -->
<h1>Polygon on the Map</h1>
<p>This example displays a polygon with points inside and outside the polygon with clustering.<br></p>
<hr/>
<div id="map" style="width: 600px; height: 400px; background: grey"></div>

CSS

code, h1, h2, h3{
  color: #124191;
  letter-spacing: -0.03em;
}
body {
  font-family:"Lucida Grande",Tahoma,sans-serif;
  color: rgb(102, 102, 102);
  font-size: 80%;
}
b, strong {
  color: black;
}
code {
  font-weight: 700;
  font-size: 120%;
}

JavaScript

/**
 * Adds a polygon to the map
 *
 * @param  {H.Map} map      A HERE Map instance within the application
 */
function addPolygonToMap(map) {
  var geoStrip = new H.geo.LineString(
    [48.8, 13.5, 100, 48.4, 13.0, 100, 48.4, 13.5, 100]
  );
  
  var polygon = new H.map.Polygon(geoStrip, {
      style: {
        strokeColor: '#829',
        lineWidth: 8
      },
      data: 'polygon'
    });
  //map.addObject(polygon);
  return polygon;
}

function logObjectsInPolygon(map, polygon){
 var geoPolygon = polygon.getGeometry();
 map.getObjectsWithin(geoPolygon, (o) => {
 				console.log('found mapObjects: '+ o.length);
        o.forEach(x=>{
        	if(typeof x.getData === 'function'){
            console.log(x.getData());
        	}
        });
  });
}

function isPointInPolygon(testPoint, polygPoints) {
    let result = false;
    let j = polygPoints.length - 1;
    for(i=0,len=j+1; i<len; i++){
    		let p = polygPoints[i];
        let lP = polygPoints[j];
        if(p.y < testPoint.y && lP.y >= testPoint.y || lP.y < testPoint.y && p.y >= testPoint.y){
            if((p.x + (testPoint.y - p.y) / (lP.y - p.y) * (lP.x - p.x)) < testPoint.x){
                result = !result;
            }
        }
        j = i;
     }
   return result;
}


/**
 * Boilerplate map initialization code starts below:
 */

//Step 1: initialize communication with the platform
var platform = new H.service.Platform({
  apikey: 'H6XyiCT0w1t9GgTjqhRXxDMrVj9h78ya3NuxlwM7XUs',
  useCIT: true,
  useHTTPS: true
});
var defaultLayers = platform.createDefaultLayers();

//Step 2: initialize a map - this map is centered over Europe
var map = new H.Map(document.getElementById('map'),
  defaultLayers.raster.normal.map,{
  center: {lat:48.5, lng:13.45},
  zoom: 10
});

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