2

我目前有一个LatLng以公里或英里为单位的位置和半径。如何使用Mapbox Android SDKCircleLayer在地图上画一个圆圈(使用或任何其他方法)?

4

1 回答 1

6

您可以使用 aPolyline并计算点来塑造一个圆:

public static void drawCircle(MapboxMap map, LatLng position, int color, double radiusMeters) {
    PolylineOptions polylineOptions = new PolylineOptions();
    polylineOptions.color(color);
    polylineOptions.width(0.5f); // change the line width here
    polylineOptions.addAll(getCirclePoints(position, radiusMeters));
    map.addPolyline(polylineOptions);
}

private static ArrayList<LatLng> getCirclePoints(LatLng position, double radius) {
    int degreesBetweenPoints = 10; // change here for shape
    int numberOfPoints = (int) Math.floor(360 / degreesBetweenPoints);
    double distRadians = radius / 6371000.0; // earth radius in meters
    double centerLatRadians = position.getLatitude() * Math.PI / 180;
    double centerLonRadians = position.getLongitude() * Math.PI / 180;
    ArrayList<LatLng> polygons = new ArrayList<>(); // array to hold all the points
    for (int index = 0; index < numberOfPoints; index++) {
        double degrees = index * degreesBetweenPoints;
        double degreeRadians = degrees * Math.PI / 180;
        double pointLatRadians = Math.asin(sin(centerLatRadians) * cos(distRadians)
                + cos(centerLatRadians) * sin(distRadians) * cos(degreeRadians));
        double pointLonRadians = centerLonRadians + Math.atan2(sin(degreeRadians)
                        * sin(distRadians) * cos(centerLatRadians),
                cos(distRadians) - sin(centerLatRadians) * sin(pointLatRadians));
        double pointLat = pointLatRadians * 180 / Math.PI;
        double pointLon = pointLonRadians * 180 / Math.PI;
        LatLng point = new LatLng(pointLat, pointLon);
        polygons.add(point);
    }
    // add first point at end to close circle
    polygons.add(polygons.get(0));
    return polygons;
}
于 2017-10-12T05:38:29.160 回答