android - 画圈 Android MapBox SDK

标签 android mapbox

我使用 Android MapBox SDK 5.1.0-SNAPSHOT。如何用坐标和以米为单位的半径绘制圆?

有绘制多边形的想法。但这是非常有问题的。

最佳答案

正如 Zugaldia 所回答的那样,绘制圆形图层是最简单的方法。但前提是你想画一个圆,忽略尺寸精度。

另一种选择是围绕您的点绘制一个圆周,以获得正确的距离和可视化效果(如果倾斜),稍后我会解释。

Kotlin ,对不起,对不起。

第一部分,绘制圆形图层:

mapView.getMapAsync {
    map = it

    // Add the source (at start, an 'empty' GeoJsonSource)
    map?.addSource(GeoJsonSource(SOURCE_ID))

    // Create a circle layer from previously defined source 
    // don't forget source identifier
    val layer = CircleLayer(CIRCLE_LAYER_ID, SOURCE_ID)
    layer.withProperties(
            circleRadius(50f),
            circleOpacity(.4f),
            circleColor(Color.WHITE)
    )

    map?.addLayer(layer)
}

当你拥有你想要绘制的位置时:

private fun updateCircleLayer() {
    // Update the GeoJsonSource
    // !!! Beware, (longitude, latitude), not the other way around !!!
    val center = Point.fromCoordinates(doubleArrayOf(longitude, latitude))
    map?.getSourceAs<GeoJsonSource>(SOURCE_ID)?.setGeoJson(center)
}

第二部分,绘制周长:

我调整了这个算法以适应语言和需求:https://stackoverflow.com/a/39006388/4258214

private fun getPerimeterFeature(radiusInKilometers: Double = .05, sides: Int = 64): Feature {
    // here, currentPosition is a class property, get your lat & long as you'd like
    val latitude = currentPosition.latitude
    val longitude = currentPosition.longitude

    val positions = mutableListOf<Position>()

    // these are conversion constants
    val distanceX: Double = radiusInKilometers / (111.319 * Math.cos(latitude * Math.PI / 180))
    val distanceY: Double = radiusInKilometers / 110.574

    val slice = (2 * Math.PI) / sides

    var theta: Double
    var x: Double
    var y: Double
    var position: Position
    for (i in 0..sides) {
        theta = i * slice
        x = distanceX * Math.cos(theta)
        y = distanceY * Math.sin(theta)

        position = Position.fromCoordinates(longitude + x, latitude + y)
        positions.add(position)
    }

    return Feature.fromGeometry(Polygon.fromCoordinates(listOf(positions)))
}

请参阅第一部分中的 updateCircleLayer() 以将返回的 Feature 提供给 GeoJSonSource,仅此而已。

希望这会有所帮助。玩得开心!

关于android - 画圈 Android MapBox SDK,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44283076/

相关文章:

java - 我如何在 Skype 上玩 google?

android - 使用底部导航 android kotlin 在 fragment 中实现 MVVM 的最佳实践

关于更改方向的 Android Fragment 生命周期问题

three.js - 从 Three.js v122 到 v123 的更改隐藏了 Mapbox 自定义图层上的阴影

javascript - 传单未将平铺层添加到底部

swift - 可能没有解决方案的奇怪的 Swift TextField 故障?

android - 设备在谷歌 Nexus 手机中没有包 com.google.android.gsf

javascript - Mapbox 切换多层的可见性

javascript - Mapbox 在圆圈上添加背景图片

Android ListView - 项目准备好后如何执行代码?