Calculate geo position based on arbitary distance in LineString
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">
<title>Calculate geo position based on arbitary distance in LineString</title>
<link rel="stylesheet" type="text/css" href="https://js.api.here.com/v3/3.1/mapsjs-ui.css" />
<link rel="stylesheet" type="text/css" href="demo.css" />
<link rel="stylesheet" type="text/css" href="styles.css" />
<link rel="stylesheet" type="text/css" href="../template.css" />
<script type="text/javascript" src='../test-credentials.js'></script>
<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>
</head>
<body id="markers-on-the-map">
<div class="page-header">
<h4>Calculate geo position based on arbitary distance in LineString</h4>
</div>
<div id="map"></div>
</body>
</html>
CSS
#map {
width: 95%;
height: 400px;
background: grey;
}
#panel {
width: 100%;
height: 400px;
}
JavaScript
getDistancePosition = function(lineString, distanceTreshold) {
let latLngAlts = lineString.getLatLngAltArray(),
previousDistance = 0;
distance = 0;
// each geo point in latLngAlts array is represented as lat, lng, alt
// therefore we need to increase counter by 3 for each loop cycle (we ignore alt values)
for (let i = 0; i < latLngAlts.length - 5; i = i+3) {
let point1 = new H.geo.Point(latLngAlts[i], latLngAlts[i + 1]),
point2 = new H.geo.Point(latLngAlts[i + 3], latLngAlts[i + 4]);
distance += point1.distance(point2);
if (distance >= distanceTreshold) {
// calculate angle between two geographical points
// used answer from https://stackoverflow.com/questions/3809179/angle-between-2-gps-coordinates
let dy = point2.lat - point1.lat,
dx = Math.cos(Math.PI/180*point1.lat)*(point2.lng - point1.lng),
angle = Math.atan2(dy, dx)*180/Math.PI,
bearing = 90 - angle,
remainingDistance = distanceTreshold - previousDistance;
// create geo point based on angle and remainingDistance
distancePoint = point1.walk(bearing, remainingDistance);
// return the calculated geo point
/* alert('Result is in distance of ' + Math.round(remainingDistance) + ' meters from point ' + i/3) */;
return distancePoint;
} else {
previousDistance = distance;
}
}
alert('could not find the distance in provided LineString (max distance: ' + distance + ')');
return null;
}
// create map
var platform = new H.service.Platform({
apikey: window.apikey
}),
defaultLayers = platform.createDefaultLayers(),
map = new H.Map(document.getElementById('map'),
defaultLayers.vector.normal.map,{
center: {lat:52.5159, lng:13.3777},
zoom: 14,
pixelRatio: window.devicePixelRatio || 1
}),
behavior = new H.mapevents.Behavior(new H.mapevents.MapEvents(map)),
ui = H.ui.UI.createDefault(map, defaultLayers),
lineString,
polyline;
window.addEventListener('resize', () =>...