gpt4 book ai didi

javascript - 无法在 openstreetmap 中用颜色填充国家/地区

转载 作者:行者123 更新时间:2023-12-03 03:36:48 24 4
gpt4 key购买 nike

enter image description here

我正在尝试用任何颜色填充国家。我正在加载国家/地区边界,例如:http://www.openstreetmap.org/api/0.6/relation/148838/full ,但它不起作用。边框已着色,但图形未填充。

我正在使用以下代码:

<!DOCTYPE html>
<html lang="en">
<head>
</head>

<body>
<div style="margin: 30px;">
<link rel="stylesheet" href="https://openlayers.org/en/v4.3.1/css/ol.css" type="text/css">
<script src="https://cdn.polyfill.io/v2/polyfill.min.js"></script>
<script src="https://openlayers.org/en/v4.3.1/build/ol.js"></script>
<div id="map" class="map" style="width: 500px; height: 500px;"></div>
<script>
var map = new ol.Map({
layers: [
new ol.layer.Tile({
source: new ol.source.OSM()
})
],
target: 'map',
view: new ol.View({
center: ol.proj.fromLonLat([0, 0]),
zoom: 1
})
});

var areaLayer = new ol.layer.Vector({
title: 'Boundaries',
source: new ol.source.Vector({
url: 'http://www.openstreetmap.org/api/0.6/relation/148838/full',
format: new ol.format.OSMXML()
}),
style: new ol.style.Style({
zIndex: 100,
stroke: new ol.style.Stroke({
color: 'rgba(246, 99, 79, 1.0)',
width: 3
}),
fill: new ol.style.Fill({
color: 'rgba(246, 99, 79, 1.0)'
})
})
});

map.addLayer(areaLayer);
</script>

</div>
</body>
</html>

如何给国家填充颜色?

最佳答案

您调用的 OSM API 返回线串几何图形,该几何图形不支持填充颜色。

如果您好奇,可以按如下方式查看 geom:

areaLayer.getSource().getFeatures()[0].getGeometry()

我还建议您在开发时使用调试版本(ol-debug.js)。

关于javascript - 无法在 openstreetmap 中用颜色填充国家/地区,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/45819494/

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