Mapbox SDK v10:如何通过矩形形状定义离线区域几何
Mapbox SDK V10 如何通过 tileRegionLoadOptions 定义矩形离线区域
Mapbox SDK V10 用几何区域替代了旧版本的矩形离线区域定义逻辑,要实现矩形离线区域,只需将矩形的四个顶点构建为**Polygon(多边形)**类型的Geometry,传入TileRegionLoadOptions即可。
Android(Kotlin)实现
使用Polygon.fromLngLats()方法,按闭合顺序传入矩形的四个顶点,构建Polygon对象后作为geometry参数:
// 定义矩形的四个坐标点(按顺时针/逆时针闭合顺序排列,最后重复起点确保闭合) val rectangleCoords = listOf( Point.fromLngLat(-8.226762635, 41.95699448), Point.fromLngLat(-8.244230531, 41.95699448), Point.fromLngLat(-8.244230531, 41.92861862), Point.fromLngLat(-8.226762635, 41.92861862), Point.fromLngLat(-8.226762635, 41.95699448) ) // 构建Polygon几何对象 val rectangleGeometry = Polygon.fromLngLats(listOf(rectangleCoords)) // 创建TileRegionLoadOptions val tileRegionLoadOptions = TileRegionLoadOptions.Builder() .geometry(rectangleGeometry) .descriptors(listOf(tilesetDescriptor)) // 替换为你的tilesetDescriptor实例 .metadata(Value(TILE_REGION_METADATA)) // 替换为你的自定义元数据 .build()
iOS(Swift)实现
创建Polygon类型的Geometry,同样按闭合顺序传入矩形顶点坐标:
// 定义矩形的四个坐标点(按闭合顺序排列,最后重复起点确保闭合) let coordinate1 = CLLocationCoordinate2D(latitude: 41.95699448, longitude: -8.226762635) let coordinate2 = CLLocationCoordinate2D(latitude: 41.95699448, longitude: -8.244230531) let coordinate3 = CLLocationCoordinate2D(latitude: 41.92861862, longitude: -8.244230531) let coordinate4 = CLLocationCoordinate2D(latitude: 41.92861862, longitude: -8.226762635) // 构建Polygon几何对象 let polygon = Polygon(coordinates: [coordinate1, coordinate2, coordinate3, coordinate4, coordinate1]) let rectangleGeometry = Geometry(polygon: polygon) // 创建TileRegionLoadOptions let tileRegionLoadOptions = TileRegionLoadOptions( geometry: rectangleGeometry, descriptors: [], // 替换为你的tileset descriptors数组 metadata: metadata, // 替换为你的自定义元数据 acceptExpired: true )
关键注意点
- 坐标点必须按闭合顺序排列(顺时针/逆时针均可),且最后要重复第一个坐标,保证多边形结构合法。
- 注意坐标格式:Android使用
(经度, 纬度),iOS的CLLocationCoordinate2D是纬度在前、经度在后,不要混淆顺序。
内容的提问来源于stack exchange,提问作者Bernardo Henriques
相关产品推荐
相关产品推荐

