gpt4 book ai didi

javascript - 开放层 : Can't add a linestring of coordinates to a map

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

我在显示基于现有坐标列表的线串时遇到问题,希望得到一些帮助。下面是我的代码,它显示了 OpenLayers5 map ,但没有覆盖线串。

我读过很多不同的主题( OpenLayers 3: simple LineString example ) 和 API 文档,但我无法弄清楚我缺少什么。

map with no linestring overlay

<!doctype html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css" type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
</head>

<body>
<div id="map" class="map"></div>

<script type="text/javascript">
var view = new ol.View({
center: ol.proj.fromLonLat([10,50]),
zoom: 14
})

//Dummy coords
var coordinates = [
[10, 50],
[11, 51],
[12, 55]
];

var layerLines = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: new ol.geom.LineString(coordinates),
name: 'Line'
})]
}),
style : new ol.style.Style({
stroke : new ol.style.Stroke({
strokeColor: '#ff0000',
strokeWidth: 5
})
})
});

var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: view
});

map.addLayer(layerLines);

</script>

</body>
</html>

JSFiddle link

最佳答案

两个错误。首先,它是宽度和颜色,而不是笔划宽度/颜色。其次,您将中心从经/纬度重新投影到 WebMercator,但忘记对线坐标执行相同的操作 - 这样您的线实际上位于几内亚湾的某个位置(距离 0,0 点 10/50 米)。

这是更正后的版本。

<!doctype html>
<html>
<head>
<meta charset="UTF-8">

<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/css/ol.css"
type="text/css">
<script src="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.3.0/build/ol.js"></script>
</head>

<body>
<div id="map" class="map"></div>

<script>
var view = new ol.View({
center: ol.proj.fromLonLat([10, 50]),
zoom: 14
})

//Dummy coords
var coordinates = [
ol.proj.fromLonLat([10, 50]),
ol.proj.fromLonLat([11, 51]),
ol.proj.fromLonLat([12, 55])
];

var layerLines = new ol.layer.Vector({
source: new ol.source.Vector({
features: [new ol.Feature({
geometry: new ol.geom.LineString(coordinates),
name: 'Line'
})]
}),
style: new ol.style.Style({
stroke: new ol.style.Stroke({
color: '#ff0000',
width: 3
})
})
});

var map = new ol.Map({
target: 'map',
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
view: view
});

map.addLayer(layerLines);
</script>

</body>
</html>

关于javascript - 开放层 : Can't add a linestring of coordinates to a map,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/55550581/

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