当我在地图上添加新标记时,我添加了一个属性以便能够使用自定义图标。然后我保存了geoJson,但是当我看到保存的geojson时,我看到prperty设置在错误的位置。
这是代码:
function updtateMarker(t, layer) {
// t is the value I need to save in property "iconSet"
// layer is the layer ID
var marker = featureGroup._layers[layer];
console.log("### updtate Marker ###");
// if marker already exists it works good
if (marker.feature && marker.feature.properties && marker.feature.properties.iconSet) {
marker.feature.properties.iconSet = t;
marker.setIcon(arrayIcon[t]); // update icon on Map
} else {
// Marker is new, it doesn't exist
// update marker icon on Map
marker.setIcon(runnaloIcon[t]);
marker.feature = {properties: {iconSet: t}};
// ***** THIS IS WRONG: why? ******
// this will be saved in "geometry" !??!
}
}
这是错误的 geojson:
{
"type": "Feature",
"properties": {},
"geometry": {
"properties": {
"iconSet": 2
},
"geometry": {
"type": "Point",
"coordinates": [9.672668, 44.934964]
}
}
这就是我需要的:
{
"type": "Feature",
"properties": {
"iconSet": 2
},
"geometry": {
"type": "Point",
"coordinates": [9.672668, 44.934964]
}
}
只是为了完成我的代码,这是保存部分
var data = featureGroup.toGeoJSON();
var convertedData = JSON.stringify(data);
感谢您的帮助!
答案 0 :(得分:0)
I fixed it adding the property when I created the new Marker instead of when using it forst time.
map.on('draw:created', function (event) {
// Each time a featuree is created, it's added to the over arching feature group
if (event.layerType == "marker") {
var layer = event.layer;
// set the first property
feature = layer.feature = layer.feature || {};
feature.type = feature.type || "Feature";
var props = feature.properties = feature.properties || {};
props.iconSet = 1; // default value
}
})