You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

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

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.08.02 23:30:54