显示跨越180度子午线的线条
显示跨越180度子午线的线条;
<!DOCTYPE html>
<html lang="en">
<head>
<title>显示跨越180度子午线的线条</title>
<meta property="og:description" content="显示跨越180度子午线的线条" />
<meta charset='utf-8'>
<meta name="viewport" content="width=device-width, initial-scale=1">
<link rel='stylesheet' href='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.css' />
<script src='https://unpkg.com/maplibre-gl@5.5.0/dist/maplibre-gl.js'></script>
<style>
body { margin: 0; padding: 0; }
html, body, #map { height: 100%; }
</style>
</head>
<body>
<div id="map"></div>
<script>
/*
当一条线跨越了国际日期变更线(180度子午线)时,通常需要特殊处理以确保它正确显示。
MapLibre GL JS会自动检测并处理横跨180度子午线的线条。
这个例子展示了跨越太平洋的航线。
*/
const map = new maplibregl.Map({
container: 'map',
style: 'https://api.maptiler.com/maps/streets/style.json?key=get_your_own_OpIi9ZULNHzrESv6T2vL',
center: [170, 10],
zoom: 1,
});
map.on('load', () => {
// 添加一条从东京到洛杉矶的航线
map.addSource('flight-route', {
'type': 'geojson',
'data': {
'type': 'Feature',
'properties': {},
'geometry': {
'type': 'LineString',
'coordinates': [
[139.767125, 35.681236], // 东京
[180, 31], // 经过日期变更线
[-118.243683, 34.052235] // 洛杉矶
]
}
}
});
map.addLayer({
'id': 'flight-route',
'type': 'line',
'source': 'flight-route',
'layout': {
'line-join': 'round',
'line-cap': 'round'
},
'paint': {
'line-color': '#FF0000',
'line-width': 3,
'line-dasharray': [2, 1]
}
});
// 添加起点和终点标记
new maplibregl.Marker({ color: '#FF0000' })
.setLngLat([139.767125, 35.681236])
.setPopup(new maplibregl.Popup().setText('东京'))
.addTo(map);
new maplibregl.Marker({ color: '#FF0000' })
.setLngLat([-118.243683, 34.052235])
.setPopup(new maplibregl.Popup().setText('洛杉矶'))
.addTo(map);
});
</script>
</body>
</html>