javascript - 如何从 GeoJSON 数据中设置自定义标记图标
问题描述
我收到来自服务器的 GeoJSON 格式的响应,并将其全部添加到地图中
$.ajax({
url: '/coupling/select/',
type: 'post',
data: {
sw_lat: southWest.lat,
sw_lon: southWest.lng,
ne_lat: northEast.lat,
ne_lon: northEast.lng
},
success: function (data) {
L.geoJSON(data, {
icon: leafletIcon,
onEachFeature: onEachFeature
}).addTo(map)
},
complete: function () {
loader_countdown_dec('coupling');
}
});
代码功能leafletIcon:
function leafletIcon(feature){
if(feature.properties.obj == 1){
return L.icon({iconUrl: 'images/cable.png'});
}
else if(feature.properties.obj == 2){
return L.icon({iconUrl: 'images/boxpon.png'});
}
else if(feature.properties.obj == 3){
return L.icon({iconUrl: 'images/coupling.png'});
}
}
我不知道如何根据对象编号更改图标。感谢帮助!
解决方案
您必须将图标直接添加到标记:
L.geoJSON(data, {
pointToLayer: pointToLayer,
onEachFeature: onEachFeature
}).addTo(map)
function pointToLayer(feature, latlng) {
return L.marker(latlng, {icon: leafletIcon(feature)});
}
推荐阅读
- navigation - 在导航地图 MAPBOX ANDROID 中旋转用户的三角形
- asp.net - 请求被中止
- django - 在 Django 中动态连接到数据库
- python - 如何在 python 中进行全局导入?
- php - php在树枝代码中转换多维数组
- shopware - Shopware 5:导入时出错:未找到类别
- jmeter - 如何通过詹金斯通过远程服务器运行jmeter测试
- javascript - 如何根据滚动找到 DIV 的 id?
- powerbuilder - PowerBuilder Datawindow 在打开时崩溃
- java - spring 调度程序甚至在服务器启动之前就开始运行