gpt4 book ai didi

javascript - 使用方形而不是点

转载 作者:行者123 更新时间:2023-11-29 22:02:39 25 4
gpt4 key购买 nike

我正在使用 leafletjs 在 map 上显示一些点。数据源是 geoJSON

this他们网站上提供的简单示例。

enter image description here

是否可以通过样式使用方形而不是点,或者唯一的方法是使用 multiPolygon 而不是 multiPoint

最佳答案

希望对您有所帮助:

HTML

<div id="map" style="width: 600px; height: 400px"></div>

JavaScript

var map = L.map('map').setView([40, -100], 15),
createSquare = function (latlng, options) {
var point = map.latLngToContainerPoint(latlng),
size = options.radius || options.size || 10,
point1 = L.point(point.x - size, point.y - size),
point2 = L.point(point.x + size, point.y + size),
latlng1 = map.containerPointToLatLng(point1),
latlng2 = map.containerPointToLatLng(point2);
return new L.rectangle([latlng1, latlng2], options);
},
points1 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-100, 40]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-99.999, 40]
}
}
]
},
points2 = {
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-100, 39.999]
}
},
{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-99.999, 39.999]
}
}
]
},
layer1 = L.geoJson(points1, {
pointToLayer: function (feature, latlng) {
return new L.CircleMarker(latlng, {
radius: 10,
color: "#000",
weight: 1,
fillColor: "#F50",
fillOpacity: 0.75
});
}
}).addTo(map),
layer2 = L.geoJson(points2, {
pointToLayer: function (feature, latlng) {
return createSquare(latlng, {
radius: 10,
color: "#000",
weight: 1,
fillColor: "#0F5",
fillOpacity: 0.75
});
}
}).addTo(map);

结果

enter image description here

Here是 jsfiddle。

关于javascript - 使用方形而不是点,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/22742642/

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