OpenLayers绘制多边形解析GeoJSON报错:feature.getId is not a function
解决OpenLayers绘制多边形时
feature.getId is not a function错误 错误根源是writeFeatureObject方法的参数使用错误:该方法仅接受单个Feature对象,但你传入了包含Feature的数组[e.feature],数组本身没有getId方法,因此触发报错。
修正后的代码:
const drawInteraction = new Draw({ type:'Polygon', freehand:true }) map.addInteraction(drawInteraction); drawInteraction.on('drawend',function(e){ let parser= new GeoJSON(); // 移除数组包裹,直接传入单个feature对象 let drawnFeatures = parser.writeFeatureObject(e.feature); console.log(drawnFeatures); })
如果需要批量处理多个Feature,改用writeFeaturesObject方法(参数为Feature数组):
// 批量处理示例 let drawnFeatures = parser.writeFeaturesObject([featureA, featureB]);
内容的提问来源于stack exchange,提问作者panos9087
相关产品推荐
相关产品推荐

