我需要将自定义道具添加到我创建的多边形中。目前,当用户在工具栏中选择多边形并创建一个形状时,在创建事件中我将其转换为 json 将其从地图中删除,将自定义道具添加到 json 并重新加载新创建的图层。
this.map.on('pm:create', e => {
const id = getUID();
const leafId = e.layer._leaflet_id;
const featureGroup = L.featureGroup().addLayer(e.layer);
this.map.eachLayer(layer => {
if (layer._leaflet_id === leafId) {
this.map.removeLayer(layer);
}
});
const data = featureGroup.toGeoJSON();
data.features[0].properties = {
id,
name: `Zone ${id}`
};
this.zoneService.add({id, data: JSON.stringify(data)})
.pipe(
switchMap((res) => this.zoneService.getAll().pipe(this.addToMap(this.map)))
).subscribe();
});
这是有效的,但我觉得我在这里没有做任何事情。添加删除添加,必须有更好的方法。谢谢你的帮助