|
|
<!DOCTYPE html> |
|
|
<html> |
|
|
<head> |
|
|
<title>OpenLayers Route</title> |
|
|
<link rel="stylesheet" href="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/css/ol.css"> |
|
|
<script src="https://cdn.jsdelivr.net/gh/openlayers/openlayers.github.io@master/en/v6.5.0/build/ol.js"></script> |
|
|
</head> |
|
|
<body> |
|
|
<div id="map" style="width: 100%; height: 400px;"></div> |
|
|
<script> |
|
|
const map = new ol.Map({ |
|
|
target: 'map', |
|
|
layers: [ |
|
|
new ol.layer.Tile({ |
|
|
source: new ol.source.OSM() |
|
|
}) |
|
|
], |
|
|
view: new ol.View({ |
|
|
center: ol.proj.fromLonLat([2.346757, 48.853005]), |
|
|
zoom: 13.5 |
|
|
}) |
|
|
}); |
|
|
|
|
|
const centerPoint = [2.346757, 48.853005]; |
|
|
|
|
|
const markersLayer = new ol.layer.Vector({ |
|
|
source: new ol.source.Vector() |
|
|
}); |
|
|
map.addLayer(markersLayer); |
|
|
|
|
|
const centerMarker = new ol.Feature({ |
|
|
geometry: new ol.geom.Point(ol.proj.fromLonLat(centerPoint)) |
|
|
}); |
|
|
|
|
|
centerMarker.setStyle(new ol.style.Style({ |
|
|
image: new ol.style.RegularShape({ |
|
|
points: 5, |
|
|
radius: 12, |
|
|
radius2: 6, |
|
|
fill: new ol.style.Fill({ |
|
|
color: '#ffff00' |
|
|
}), |
|
|
stroke: new ol.style.Stroke({ |
|
|
color: '#ff0000', |
|
|
width: 2 |
|
|
}) |
|
|
}), |
|
|
text: new ol.style.Text({ |
|
|
text: 'Center', |
|
|
fill: new ol.style.Fill({ |
|
|
color: '#000' |
|
|
}), |
|
|
stroke: new ol.style.Stroke({ |
|
|
color: '#fff', |
|
|
width: 2 |
|
|
}), |
|
|
font: 'bold 14px Arial', |
|
|
offsetY: -20 |
|
|
}) |
|
|
})); |
|
|
|
|
|
const centerLayer = new ol.layer.Vector({ |
|
|
source: new ol.source.Vector() |
|
|
}); |
|
|
|
|
|
centerLayer.getSource().addFeature(centerMarker); |
|
|
|
|
|
centerLayer.setZIndex(2); |
|
|
|
|
|
const route1 = [ |
|
|
[2.346757, 48.853005], |
|
|
[2.33877, 48.85258], |
|
|
[2.33293, 48.85262], |
|
|
[2.33274, 48.85259], |
|
|
[2.32914, 48.8477], |
|
|
[2.328138, 48.847357], |
|
|
[2.32867, 48.84551], |
|
|
[2.3346, 48.84434], |
|
|
[2.33315, 48.841244], |
|
|
[2.346757, 48.853005] |
|
|
]; |
|
|
|
|
|
const route2 = [ |
|
|
[2.346757, 48.853005], |
|
|
[2.34412, 48.8498], |
|
|
[2.33726, 48.84093], |
|
|
[2.3368, 48.84007], |
|
|
[2.34821, 48.84168], |
|
|
[2.35153, 48.84051], |
|
|
[2.35239, 48.84435], |
|
|
[2.35383, 48.852], |
|
|
[2.35814, 48.85371], |
|
|
[2.346757, 48.853005] |
|
|
]; |
|
|
|
|
|
const route3 = [ |
|
|
[2.346757, 48.853005], |
|
|
[2.36475, 48.85588], |
|
|
[2.36632, 48.85765], |
|
|
[2.36846, 48.86596], |
|
|
[2.35429, 48.85945], |
|
|
[2.35038, 48.8582], |
|
|
[2.34454, 48.85837], |
|
|
[2.344538, 48.858366], |
|
|
[2.34676, 48.85301], |
|
|
[2.346757, 48.853005] |
|
|
]; |
|
|
|
|
|
const route4 = [ |
|
|
[2.346757, 48.853005], |
|
|
[2.340631, 48.865014], |
|
|
[2.34063, 48.86501], |
|
|
[2.34133, 48.86026], |
|
|
[2.33892, 48.86309], |
|
|
[2.32946, 48.85789], |
|
|
[2.346757, 48.853005] |
|
|
]; |
|
|
|
|
|
const routes = [route1, route2, route3, route4]; |
|
|
const colors = ['#E69F00', '#56B4E9', '#009E73', '#CC79A7']; |
|
|
routes.forEach((route, routeIndex) => { |
|
|
route.forEach((coord, pointIndex) => { |
|
|
if (pointIndex > 0 && pointIndex < route.length - 1) { |
|
|
const marker = new ol.Feature({ |
|
|
geometry: new ol.geom.Point(ol.proj.fromLonLat(coord)) |
|
|
}); |
|
|
|
|
|
marker.setStyle(new ol.style.Style({ |
|
|
image: new ol.style.Circle({ |
|
|
radius: 6, |
|
|
fill: new ol.style.Fill({ |
|
|
color: colors[routeIndex] |
|
|
}), |
|
|
stroke: new ol.style.Stroke({ |
|
|
color: '#ffffff', |
|
|
width: 2 |
|
|
}) |
|
|
}), |
|
|
text: new ol.style.Text({ |
|
|
text: `R${routeIndex + 1}-${pointIndex}`, |
|
|
fill: new ol.style.Fill({ |
|
|
color: '#fff' |
|
|
}), |
|
|
stroke: new ol.style.Stroke({ |
|
|
color: '#000', |
|
|
width: 2 |
|
|
}), |
|
|
font: '12px Arial', |
|
|
offsetY: -15 |
|
|
}) |
|
|
})); |
|
|
|
|
|
markersLayer.getSource().addFeature(marker); |
|
|
} |
|
|
}); |
|
|
}); |
|
|
|
|
|
routes.forEach((route, index) => { |
|
|
fetch( |
|
|
'https://router.project-osrm.org/route/v1/driving/' + |
|
|
route.join(';') + |
|
|
'?overview=full&geometries=polyline6' |
|
|
).then(function (response) { |
|
|
response.json().then(function (result) { |
|
|
const polyline = result.routes[0].geometry; |
|
|
const routeGeom = new ol.format.Polyline({ |
|
|
factor: 1e6, |
|
|
}).readGeometry(polyline, { |
|
|
dataProjection: 'EPSG:4326', |
|
|
featureProjection: map.getView().getProjection(), |
|
|
}); |
|
|
|
|
|
const routeFeature = new ol.Feature({ |
|
|
geometry: routeGeom, |
|
|
name: 'Route ' + (index + 1) |
|
|
}); |
|
|
|
|
|
const vectorLayer = new ol.layer.Vector({ |
|
|
source: new ol.source.Vector({ |
|
|
features: [routeFeature] |
|
|
}), |
|
|
style: new ol.style.Style({ |
|
|
stroke: new ol.style.Stroke({ |
|
|
color: colors[index], |
|
|
width: 4 |
|
|
}) |
|
|
}) |
|
|
}); |
|
|
|
|
|
map.addLayer(vectorLayer); |
|
|
|
|
|
vectorLayer.setZIndex(1); |
|
|
}); |
|
|
}); |
|
|
}); |
|
|
|
|
|
map.addLayer(centerLayer); |
|
|
</script> |
|
|
</body> |
|
|
</html> |
|
|
|