<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Add Lines</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://tiles.locationiq.com/v3/libs/maplibre-gl/1.15.2/maplibre-gl.js'></script>
<link href='https://tiles.locationiq.com/v3/libs/maplibre-gl/1.15.2/maplibre-gl.css' rel='stylesheet' />
<script src='https://tiles.locationiq.com/v3/js/liq-styles-ctrl-libre-gl.js?v=0.1.8'></script>
<link href='https://tiles.locationiq.com/v3/css/liq-styles-ctrl-libre-gl.css?v=0.1.8' rel="stylesheet" />
<style>
body { margin:0px; padding:0px; }
#map { position:absolute; top:0px; bottom:0px; width:100%; }
</style>
</head>
<body>
<style>
.marker {
display: block;
border: none;
border-radius: 50%;
cursor: pointer;
padding: 0;
}
</style>
<div id='map'></div>
<script>
//Add your LocationIQ Maps Access Token here (not the API token!)
locationiq.key = 'pk.aa7f5d0539c5675b7f3429402939d8fa';
//Define the map and configure the map's theme
var map = new maplibregl.Map({
container: 'map',
style: locationiq.getLayer("Streets"),
zoom: 12,
center: [-122.42, 37.779]
});
//Add a Scale to the map
map.addControl(new maplibregl.ScaleControl({
maxWidth: 80,
unit: 'metric' //imperial for miles
}));
//Add markers from geojson. This list can be generated dynamically with an AJAX call as well.
var geojson = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"message": "Foo",
"iconSize": [60, 60]
},
"geometry": {
"type": "Point",
"coordinates": [
-122.487377,
37.772487
]
}
},
{
"type": "Feature",
"properties": {
"message": "Bar",
"iconSize": [50, 50]
},
"geometry": {
"type": "Point",
"coordinates": [
-122.461627,
37.752945
]
}
},
{
"type": "Feature",
"properties": {
"message": "Baz",
"iconSize": [40, 40]
},
"geometry": {
"type": "Point",
"coordinates": [
-122.431415,
37.761766
]
}
}
]
};
//Add markers to map
//https://www.mapbox.com/mapbox-gl-js/api#marker
geojson.features.forEach(function(marker) {
// create a DOM element for the marker
var el = document.createElement('div');
el.className = 'marker';
el.style.backgroundImage = 'url(https://placekitten.com/g/' + marker.properties.iconSize.join('/') + '/)';
el.style.width = marker.properties.iconSize[0] + 'px';
el.style.height = marker.properties.iconSize[1] + 'px';
//Instead of this click listener, we can attach a popup / infowindow to this marker (see next section)
el.addEventListener('click', function() {
window.alert(marker.properties.message);
});
// add marker to map
new maplibregl.Marker(el)
.setLngLat(marker.geometry.coordinates)
.addTo(map);
});
map.on('load', function () {
map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.487377, 37.772487],
[-122.461627, 37.752945],
[-122.431415, 37.761766]
]
}
}
},
"layout": {
"line-join": "round",
"line-cap": "round"
},
"paint": {
"line-color": "#B22222",
"line-width": 5
}
});
});
</script>
</body>
</html>