TrackAsiaTrackAsia GL JS DocsExamplesFit to the bounds of a LineString

Fit to the bounds of a LineString

Get the bounds of a LineString by passing its first coordinates to LngLatBounds and chaining extend to include the last coordinates.

<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8" />
<title>Fit to the bounds of a LineString</title>
<meta name="viewport" content="initial-scale=1,maximum-scale=1,user-scalable=no" />
<script src="https://unpkg.com/trackasia-gl@1.0.5/dist/trackasia-gl.js"></script>
<link href="https://unpkg.com/trackasia-gl@1.0.5/dist/trackasia-gl.css" rel="stylesheet" />
<style>
body { margin: 0; padding: 0; }
#map { position: absolute; top: 0; bottom: 0; width: 100%; }
</style>
</head>
<body>
<style>
.btn-control {
font: bold 12px/20px 'Helvetica Neue', Arial, Helvetica, sans-serif;
background-color: #3386c0;
color: #fff;
position: absolute;
top: 20px;
left: 50%;
z-index: 1;
border: none;
width: 200px;
margin-left: -100px;
display: block;
cursor: pointer;
padding: 10px 20px;
border-radius: 3px;
}
.btn-control:hover {
background-color: #4ea0da;
}
</style>
<div id="map"></div>
<button id="zoomto" class="btn-control">Zoom to bounds</button>
<script>
// A GeoJSON object with a LineString route from the White House to Capitol Hill
var geojson = {
'type': 'FeatureCollection',
'features': [
{
'type': 'Feature',
'geometry': {
'type': 'LineString',
'properties': {},
'coordinates': [[106.6366048812866,10.79873175227713],[106.63364372253417,10.79876515143842],[106.63364372253417,10.79549195896866],[106.62982425689697,10.79549195896866],[106.6240092277527,10.79387200688839],[106.61519012451171,10.791416957534205],[106.61521158218382,10.792068305429156],[106.60813055038452,10.792051604275686],[106.60832366943357,10.79143365883688],[106.60818419456481,10.79082405874451],[106.60815200805663,10.78989712255097]]
}
}
]
};
var map = new trackasiagl.Map({
container: 'map',
style: 'https://maps.track-asia.com/styles/v1/streets.json?key=public_key',
center: {"lat":10.762622,"lng":106.660172},
zoom: 12
});
map.on('load', function () {
map.addSource('LineString', {
'type': 'geojson',
'data': geojson
});
map.addLayer({
'id': 'LineString',
'type': 'line',
'source': 'LineString',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#BF93E4',
'line-width': 5
}
});
document
.getElementById('zoomto')
.addEventListener('click', function () {
// Geographic coordinates of the LineString
var coordinates = geojson.features[0].geometry.coordinates;
// Pass the first coordinates in the LineString to `lngLatBounds` &
// wrap each coordinate pair in `extend` to include them in the bounds
// result. A variation of this technique could be applied to zooming
// to the bounds of multiple Points or Polygon geomteries - it just
// requires wrapping all the coordinates with the extend method.
var bounds = coordinates.reduce(function (bounds, coord) {
return bounds.extend(coord);
}, new trackasiagl.LngLatBounds(
coordinates[0],
coordinates[0]
));
map.fitBounds(bounds, {
padding: 20
});
});
});
</script>
</body>
</html>