gpt4 book ai didi

mapbox - 如何绘制缩放时不改变的虚线

转载 作者:行者123 更新时间:2023-12-02 00:11:00 31 4
gpt4 key购买 nike

当我使用“line-dasharray”属性绘制虚线时,它的行为很奇怪 - 缩放时线的长度和间隙会发生变化。请参阅示例。问题:如何绘制具有恒定线和间隙长度的虚线,当放大/缩小时这些线和间隙长度不会改变?

//这个愚蠢的机器人说我的帖子主要是代码。不知道哪里出了问题,

//但必须添加一些愚蠢的文本行

    <!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Add a GeoJSON line</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>

<div id='map'></div>
<script>
mapboxgl.accessToken = 'pk.eyJ1IjoibHVjYXN3b2oiLCJhIjoiY2l5Nmg4cWU1MDA0ejMzcDJtNHJmZzJkcyJ9.WhcEdTYQH6sSw2pm0RSP9Q';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-122.486052, 37.830348],
zoom: 15
});

map.on('load', function () {

map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141],
[-122.48339653015138, 37.83270036637107],
[-122.48356819152832, 37.832056363179625],
[-122.48404026031496, 37.83114119107971],
[-122.48404026031496, 37.83049717427869],
[-122.48348236083984, 37.829920943955045],
[-122.48356819152832, 37.82954808664175],
[-122.48507022857666, 37.82944639795659],
[-122.48610019683838, 37.82880236636284],
[-122.48695850372314, 37.82931081282506],
[-122.48700141906738, 37.83080223556934],
[-122.48751640319824, 37.83168351665737],
[-122.48803138732912, 37.832158048267786],
[-122.48888969421387, 37.83297152392784],
[-122.48987674713133, 37.83263257682617],
[-122.49043464660643, 37.832937629287755],
[-122.49125003814696, 37.832429207817725],
[-122.49163627624512, 37.832564787218985],
[-122.49223709106445, 37.83337825839438],
[-122.49378204345702, 37.83368330777276]
]
}
}
},
"paint": {
"line-color": "#888",
"line-width": 8,
"line-dasharray": [5, 5]
}
});
});
</script>

</body>
</html>

最佳答案

嗯,我发现了一些东西可以让它好一点,但它仍然不完美,因为虚线随着缩放而继续变化。

答案的一部分是,线之间的间隙以线宽单位指定,而不是以像素为单位。当线宽恒定(以像素为单位)且不依赖于缩放时,当缩放级别发生变化时,线宽的大小(例如以米为单位)会变大或变小。这也会导致线条之间的间隙发生变化。 Here您可以找到如何制作具有可变宽度的线条的描述,该宽度取决于缩放级别并保留其相对大小。

以下是我将这种方法应用到我的示例中的结果(它变得更好,但当我放大/缩小时线条仍然发生变化):

<!DOCTYPE html>
<html>
<head>
<meta charset='utf-8' />
<title>Add a GeoJSON line</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.js'></script>
<link href='https://api.tiles.mapbox.com/mapbox-gl-js/v0.44.1/mapbox-gl.css' rel='stylesheet' />
<style>
body { margin:0; padding:0; }
#map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>

<div id='map'></div>
<script>


mapboxgl.accessToken = 'pk.eyJ1IjoibHVjYXN3b2oiLCJhIjoiY2l5Nmg4cWU1MDA0ejMzcDJtNHJmZzJkcyJ9.WhcEdTYQH6sSw2pm0RSP9Q';
var map = new mapboxgl.Map({
container: 'map',
style: 'mapbox://styles/mapbox/streets-v9',
center: [-122.486052, 37.830348],
zoom: 15
});

map.on('load', function () {
let baseWidth = 5;
let baseZoom = 15;

map.addLayer({
"id": "route",
"type": "line",
"source": {
"type": "geojson",
"data": {
"type": "Feature",
"properties": {},
"geometry": {
"type": "LineString",
"coordinates": [
[-122.48369693756104, 37.83381888486939],
[-122.48348236083984, 37.83317489144141],
[-122.48339653015138, 37.83270036637107],
[-122.48356819152832, 37.832056363179625],
[-122.48404026031496, 37.83114119107971],
[-122.48404026031496, 37.83049717427869],
[-122.48348236083984, 37.829920943955045],
[-122.48356819152832, 37.82954808664175],
[-122.48507022857666, 37.82944639795659],
[-122.48610019683838, 37.82880236636284],
[-122.48695850372314, 37.82931081282506],
[-122.48700141906738, 37.83080223556934],
[-122.48751640319824, 37.83168351665737],
[-122.48803138732912, 37.832158048267786],
[-122.48888969421387, 37.83297152392784],
[-122.48987674713133, 37.83263257682617],
[-122.49043464660643, 37.832937629287755],
[-122.49125003814696, 37.832429207817725],
[-122.49163627624512, 37.832564787218985],
[-122.49223709106445, 37.83337825839438],
[-122.49378204345702, 37.83368330777276]
]
}
}
},
"paint": {
"line-color": "#888",
"line-width":
{
'type': 'exponential',
'base': 2,
'stops': [
[0, baseWidth * Math.pow(2, (0 - baseZoom))],
[22, baseWidth * Math.pow(2, (22 - baseZoom))]
]
},
"line-dasharray": [5, 5]
}
});
});
</script>

</body>
</html>

关于mapbox - 如何绘制缩放时不改变的虚线,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/49728009/

31 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com