我使用 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/