同一Fragment不同Frame加载两个Mapbox地图时单个地图冻结求助
报错信息
原报错:Can't update annotation: Symbol{geometry=Point{type=Point, bbox=null, coordinates=[-121.3559284, 38.9228032]}, properties={"symbol-sort-key":null,"icon-size":null,"icon-image":"marker-icon-id","icon-rotate":165.30766,"icon-offset":null,"icon-anchor":null,"text-field":null,"text-font":null,"text-size":null,"text-max-width":null,"text-letter-spacing":null,"text-justify":null,"text-radial-offset":null,"text-anchor":null,"text-rotate":null,"text-transform":null,"text-offset":null,"icon-opacity":null,"icon-color":null,"icon-halo-color":null,"icon-halo-width":null,"icon-halo-blur":null,"text-opacity":null,"text-color":null,"text-halo-color":null,"text-halo-width":null,"text-halo-blur":null,"id":2,"custom_data":null}, isDraggable=false}, the annotation isn't active annotation.
翻译:无法更新标注:Symbol{geometry=Point{type=Point, bbox=null, coordinates=[-121.3559284, 38.9228032]}, properties={"symbol-sort-key":null,"icon-size":null,"icon-image":"marker-icon-id","icon-rotate":165.30766,"icon-offset":null,"icon-anchor":null,"text-field":null,"text-font":null,"text-size":null,"text-max-width":null,"text-letter-spacing":null,"text-justify":null,"text-radial-offset":null,"text-anchor":null,"text-rotate":null,"text-transform":null,"text-offset":null,"icon-opacity":null,"icon-color":null,"icon-halo-color":null,"icon-halo-width":null,"icon-halo-blur":null,"text-opacity":null,"text-color":null,"text-halo-color":null,"text-halo-width":null,"text-halo-blur":null,"id":2,"custom_data":null}, isDraggable=false},该标注并非活跃标注。
问题场景
- 初始状态:全屏MapView显示,右下角有小视频窗口,通过SymbolManager在地图上展示当前经纬度,符号可正常移动。
- 第一次切换:点击视频窗口,切换为全屏视频+小窗口MapView,小地图符号仍能正常移动。
- 第二次切换:点击小MapView切回全屏后,地图移动图标冻结,触发上述报错。
解决方案
绑定SymbolManager与MapView生命周期
切换MapView显示状态时,同步执行SymbolManager的生命周期方法。切回全屏MapView时,重新初始化对应SymbolManager,避免引用失效标注实例:if (fullScreenMapView.isShown()) { fullScreenSymbolManager = new SymbolManager(fullScreenMapView, fullScreenMapboxMap, style); updateCurrentLocationSymbol(fullScreenSymbolManager, currentLatLng); }维护独立的Symbol实例
两个MapView的SymbolManager各自维护独立的Symbol对象,切换时不为当前活跃MapView复用旧Symbol,而是创建新Symbol添加,确保标注处于活跃状态。更新前检查标注活跃性
更新标注前,先通过symbolManager.getAnnotations()确认目标Symbol在活跃列表中,存在则更新,不存在则重新添加:boolean isActive = false; for (Annotation annotation : symbolManager.getAnnotations()) { if (annotation.getId() == targetSymbolId) { isActive = true; break; } } if (isActive) { symbolManager.updateSymbol(targetSymbol); } else { symbolManager.createSymbol(targetSymbol); }切换时清理资源
切换到小窗口MapView时,清理全屏MapView的SymbolManager,移除所有标注;切回全屏时重新初始化,避免残留失效引用。
内容的提问来源于stack exchange,提问作者Kutbi

