我使用Android MapBox SDK 5.1.0-SNAPSHOT。如何绘制坐标和半径以米为单位的圆?
有想法绘制多边形多边形。但这很成问题。
答案 0 :(得分:2)
正如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
。
希望这会有所帮助。玩得开心!
答案 1 :(得分:1)
有想法绘制多边形
这是一个选项,另一个选项是为您的活动添加CircleLayer
。这是我们的Runtime Styling API的一部分,您可以找到an example here。
答案 2 :(得分:1)
根据@Xalamadrax的回答,我简化了他的代码并将其翻译成java。
private PolygonOptions generatePerimeter(LatLng centerCoordinates, double radiusInKilometers, int numberOfSides) {
List<LatLng> positions = new ArrayList<>();
double distanceX = radiusInKilometers / (111.319 * Math.cos(centerCoordinates.getLatitude() * Math.PI / 180));
double distanceY = radiusInKilometers / 110.574;
double slice = (2 * Math.PI) / numberOfSides;
double theta;
double x;
double y;
LatLng position;
for (int i = 0; i < numberOfSides; ++i) {
theta = i * slice;
x = distanceX * Math.cos(theta);
y = distanceY * Math.sin(theta);
position = new LatLng(centerCoordinates.getLatitude() + y,
centerCoordinates.getLongitude() + x);
positions.add(position);
}
return new PolygonOptions()
.addAll(positions)
.fillColor(Color.BLUE)
.alpha(0.4f);
}
并将其添加到地图中:
mMapView.getMapAsync(new OnMapReadyCallback() {
@Override
public void onMapReady(MapboxMap mapboxMap) {
mapboxMap.addPolygon(generatePerimeter(
new LatLng(48.8566d, 2.3522d),
100,
64));
}
});
编辑:我对mapbox很新,所以也许可以改进。