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

如何在Jetpack Compose的Mapbox中获取地图四个角的坐标

获取Mapbox地图四个角落的经纬度坐标

你可以通过Mapbox Map的projection对象获取当前视口的边界范围,再从边界中提取四个角落的经纬度。具体操作如下:

在你的mapIdleCallback里,调用mapboxMap.projection.getBounds()拿到LatLngBounds对象,这个对象提供了直接获取四个角落坐标的方法:

  • northwest():对应**左上(西北)**角坐标
  • northeast():对应**右上(东北)**角坐标
  • southwest():对应**左下(西南)**角坐标
  • southeast():对应**右下(东南)**角坐标

修改后的代码示例:

mapView.mapboxMap.subscribeMapIdle(mapIdleCallback = {
    val bounds = mapView.mapboxMap.projection.getBounds()
    val topLeft = bounds.northwest()
    val topRight = bounds.northeast()
    val bottomLeft = bounds.southwest()
    val bottomRight = bounds.southeast()

    Log.d("MapBounds", "左上: $topLeft")
    Log.d("MapBounds", "右上: $topRight")
    Log.d("MapBounds", "左下: $bottomLeft")
    Log.d("MapBounds", "右下: $bottomRight")
})

每个LatLng对象包含latitude和longitude属性,你可以直接通过topLeft.latitude、topLeft.longitude单独取出经纬度值。

内容的提问来源于stack exchange,提问作者Muhammad Muddassar

相关产品推荐
方舟 Agent Plan

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

最近更新时间:2026.06.25 12:15:05