搜索特定名称 onEachFeature
Search for specific name onEachFeature
我有一个看起来像这样的函数:
<script type="text/javascript">
function oef(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
opacity: 1.00,
direction: 'top',
offset: [0, -7]
});
}
}
</script>
现在我想扩展该功能并在 .geojson 文件中搜索特定名称,您可以在下面看到:
"name": "* Testing",
是的,星号是名称的一部分。如何重写我的函数以搜索此名称,以及何时在地图上找到具有该名称的线条将颜色更改为 LightSkyBlue?
我的 .geojson 文件的简短片段:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"_umap_options": {
"color": "LightSkyBlue",
"weight": "4",
"opacity": "1"
},
"name": "* Testing",
"description": "Testing this description..."
},
"geometry": {
"type": "LineString",
"coordinates": [
[
56.601563,
-60.930432
],
[
38.95752,
-52.629729
]
]
}
}
]
}
正如您可能已经注意到的,我在 umap 中制作了一些线条,但 leaflet 无法识别颜色等 umap 选项,因此我正在尝试寻找解决方法。
编辑:
忘了说我已经有了一个全局的线条“着色器”,这就是为什么我需要搜索特定线条的名称。
// geoJSON URL'S
var TestlinesURL = "https://raw.githubusercontent.com/***/Testlines.geojson"
// Stuff for lines
var TestlinesStyle = {
"color": "firebrick",
"weight": 5,
"opacity": 0.95
};
// Load .geoJSON
var Testlines = new L.GeoJSON.AJAX([TestlinesURL],{style:TestlinesStyle,onEachFeature:oef});
更新您的代码以添加硬编码样式:
function oef(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
opacity: 1.00,
direction: 'top',
offset: [0, -7]
});
if(feature.properties.name === "* Testing"){
layer.setStyle({
color: "LightSkyBlue"
})
}
}
}
但我建议读出 umap 选项:
function oef(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
opacity: 1.00,
direction: 'top',
offset: [0, -7]
});
if(feature.properties.name === "* Testing"){
layer.setStyle(feature.properties._umap_options);
}
}
}
我有一个看起来像这样的函数:
<script type="text/javascript">
function oef(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
opacity: 1.00,
direction: 'top',
offset: [0, -7]
});
}
}
</script>
现在我想扩展该功能并在 .geojson 文件中搜索特定名称,您可以在下面看到:
"name": "* Testing",
是的,星号是名称的一部分。如何重写我的函数以搜索此名称,以及何时在地图上找到具有该名称的线条将颜色更改为 LightSkyBlue?
我的 .geojson 文件的简短片段:
{
"type": "FeatureCollection",
"features": [
{
"type": "Feature",
"properties": {
"_umap_options": {
"color": "LightSkyBlue",
"weight": "4",
"opacity": "1"
},
"name": "* Testing",
"description": "Testing this description..."
},
"geometry": {
"type": "LineString",
"coordinates": [
[
56.601563,
-60.930432
],
[
38.95752,
-52.629729
]
]
}
}
]
}
正如您可能已经注意到的,我在 umap 中制作了一些线条,但 leaflet 无法识别颜色等 umap 选项,因此我正在尝试寻找解决方法。
编辑:
忘了说我已经有了一个全局的线条“着色器”,这就是为什么我需要搜索特定线条的名称。
// geoJSON URL'S
var TestlinesURL = "https://raw.githubusercontent.com/***/Testlines.geojson"
// Stuff for lines
var TestlinesStyle = {
"color": "firebrick",
"weight": 5,
"opacity": 0.95
};
// Load .geoJSON
var Testlines = new L.GeoJSON.AJAX([TestlinesURL],{style:TestlinesStyle,onEachFeature:oef});
更新您的代码以添加硬编码样式:
function oef(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
opacity: 1.00,
direction: 'top',
offset: [0, -7]
});
if(feature.properties.name === "* Testing"){
layer.setStyle({
color: "LightSkyBlue"
})
}
}
}
但我建议读出 umap 选项:
function oef(feature, layer) {
if (feature.properties && feature.properties.name) {
layer.bindTooltip(feature.properties.name, {
opacity: 1.00,
direction: 'top',
offset: [0, -7]
});
if(feature.properties.name === "* Testing"){
layer.setStyle(feature.properties._umap_options);
}
}
}