Flutter中mapbox_gl插件获取当前地图缩放级别异常求助
Flutter mapbox_gl: ~0.16.0 获取当前缩放级别始终返回初始值的问题
我在Flutter应用中使用mapbox_gl: ~0.16.0插件,需要在用户移动地图(或移动结束后)获取当前缩放级别,但它总是返回初始缩放级别而非当前值。不过获取当前视口边界时能得到正确坐标。
我的实现代码如下:
View 代码
MapboxMap( onMapCreated: (mController) { // Assign the MapboxMapController instance to mapboxMapController mapboxMapController = mController; controller.onMapCreated(mController); }, zoomGesturesEnabled: true, accessToken:"<MAPBOX ACCESS TOKEN>", initialCameraPosition: const CameraPosition( target: LatLng(47.5973305239398, -107.06058413659325), zoom: 15.0), doubleClickZoomEnabled: true, onCameraIdle: () { controller.getView(mapboxMapController); }, styleString: "mapbox://styles/mapbox/outdoors-v12", )
Controller 代码
Future<void> getView(MapboxMapController mapboxMap) async { mapboxMap.getVisibleRegion().then( (bounds) async { double northeastLat = bounds.northeast.latitude; double northeastLng = bounds.northeast.longitude; double northwestLat = bounds.northeast.latitude; double northwestLng = bounds.southwest.longitude; double southeastLat = bounds.southwest.latitude; double southeastLng = bounds.northeast.longitude; double southwestLat = bounds.southwest.latitude; double southwestLng = bounds.southwest.longitude; double zoomLevel = mapboxMap.cameraPosition!.zoom; print(zoomLevel); // 这里总是打印15.0,即初始缩放级别 }, ); }
多数文档或示例代码都建议使用这种方式,但对我无效!我查阅了Mapbox Maps文档,未找到实现该功能的清晰示例,希望能得到帮助或代码片段,谢谢!
我也尝试过以下写法,依然无效:
mapboxMap.getCameraPosition().zoom;
内容的提问来源于stack exchange,提问作者Spike Spiegel
相关产品推荐
相关产品推荐

