mapbox将坐标作为变量传递不起作用



它试图实现从外部数据绘制多边形的解决方案,如https://jsfiddle.net/zxaktouy/1/所示,但我得到的错误:给'frag15'的输入数据不是一个有效的GeoJSON对象。

我JS-method:

drawFragment : function(pFRAGMENT) {
const wPolygon =  pFRAGMENT.coordinates;
console.log("drawFragment: Coords="+wPolygon);
wSourceId = "frag"+pFRAGMENT.id;
wFillId = "fragfill"+pFRAGMENT.id;
wOutlineId = "fragoutline"+pFRAGMENT.id;
map.addSource(wSourceId,{
'type': 'geojson',
'data': {
'type': 'Feature',
'geometry': {
'type': 'Polygon',
'coordinates': [ 
wPolygon
]
}
}
});
// Add a new layer to visualize the polygon.
map.addLayer({
'id': wFillId,
'type': 'fill',
'source': wSourceId, // reference the data source
'layout': {},
'paint': {
'fill-color': '#00ff80', // green color fill
'fill-opacity': 0.5
}
});
// Add a black outline around the polygon.
map.addLayer({
'id': wOutlineId,
'type': 'line',
'source': wSourceId,
'layout': {},
'paint': {
'line-color': '#0d0',
'line-width': 2
}
});
},

传递的数据(通过console.log完美显示)如下所示:

[[8.543590974130666,47.377830192117756],
[8.543641551219707,47.37784384335191],
[8.543634914341965,47.37789513281288],
[8.543582309906242,47.37791046432616],
[8.543590974130666,47.377830192117756]]

当我替换"wPolygon"只需将数据复制粘贴到代码中,一切都可以正常工作。

替换后:

const wPolygon =  pFRAGMENT.coordinates;

var wPolygon = JSON.parse(pFRAGMENT.coordinates);

最新更新