如何强制重渲染Azure Maps地图?室内项目高亮与楼层切换失效求助
问题
在Azure Maps室内项目中开发搜索功能,可按名称检索已加载的GeoJSON图形。期望点击搜索结果时,对应房间以不同颜色高亮,因此在PolygonExtrusionLayer的数据驱动样式中编写了case语句,但修改对应属性后无效果;切换查看楼层时地图也未更新。根据官方文档,调用shape的addProperty方法应触发地图画布刷新,但实际无变化。请问是否需要反复创建和移除图层,或有其他方法可强制地图重渲染?
相关代码如下:
//Add a layer for rendering the outline of polygons. var polygonExtrusionLayer = new atlas.layer.PolygonExtrusionLayer(datasource, null, { height: ['*', ['+', 1, ['to-number', ['get', 'Bouwlaag']]], floorheight], base: ['*', ['to-number', ['get', 'Bouwlaag']], floorheight], fillOpacity: 0.8, fillColor: ['case', ['<', ['to-number', ['get', 'Bouwlaag']], currentFloor], 'lightblue', ['case', ['==', ['get', 'resultId'], resultId], 'red', 'blue' ] ], filter: ['all', ['any', ['==', ['geometry-type'], 'Polygon'], //Only render Polygon or MultiPolygon in this layer. ['==', ['geometry-type'], 'MultiPolygon'] ], ['<=', ['to-number', ['get', 'Bouwlaag']], currentFloor] ] }); function featureClicked(id) { var shape = datasource.getShapeById(id); console.log(id) resultId = id; currentFloor = Number(shape.getProperties().Bouwlaag); shape.addProperty('resultId', id); }
解决方案
- 问题核心:图层初始化时直接引用的
currentFloor和resultId变量会被静态解析到配置中,后续变量变更不会自动同步到图层,导致样式和过滤器不更新。 - 无需反复创建/移除图层,通过以下步骤即可触发地图重渲染:
- 更新图层配置
当currentFloor或resultId变化后,调用图层的setOptions方法,传入包含最新变量值的样式和过滤器配置,强制图层重新计算表达式:function featureClicked(id) { var shape = datasource.getShapeById(id); resultId = id; currentFloor = Number(shape.getProperties().Bouwlaag); // 清除之前所有图形的resultId属性,避免多个高亮 datasource.getShapes().forEach(s => { s.getProperties().resultId && s.removeProperty('resultId'); }); shape.addProperty('resultId', id); // 更新图层样式和过滤器 polygonExtrusionLayer.setOptions({ fillColor: ['case', ['<', ['to-number', ['get', 'Bouwlaag']], currentFloor], 'lightblue', ['case', ['==', ['get', 'resultId'], resultId], 'red', 'blue' ] ], filter: ['all', ['any', ['==', ['geometry-type'], 'Polygon'], ['==', ['geometry-type'], 'MultiPolygon'] ], ['<=', ['to-number', ['get', 'Bouwlaag']], currentFloor] ] }); } - 优化高亮逻辑
可以省去给图形添加resultId属性的步骤,直接在样式表达式中对比图形ID与选中ID,减少数据源操作:
此时// 修改图层的fillColor表达式 fillColor: ['case', ['<', ['to-number', ['get', 'Bouwlaag']], currentFloor], 'lightblue', ['case', ['==', ['get', 'id'], resultId], // 直接使用图形ID对比 'red', 'blue' ] ]featureClicked只需更新resultId和currentFloor,再调用setOptions即可。
- 更新图层配置
内容的提问来源于stack exchange,提问作者Erik Bussing
相关产品推荐
相关产品推荐

