且构网

分享程序员开发的那些事...
且构网 - 分享程序员编程开发的那些事

如何在Android的Mapbox SDK中绘制弯曲的折线

更新时间:2023-01-02 10:16:15

我找到了Google Maps SDK的解决方案,所以我将其转换为仅使用Mapbox SDK:

I found a solution with the google maps sdk so I converted it to use only the Mapbox sdk :

受到的启发Google Maps Android中的弧形虚线?

public static List<LatLng> computeCurvedPolyline(LatLng from, LatLng to, double k) {
    //Calculate distance and heading between two points
    double distance = from.distanceTo(to);
    double heading = computeHeading(from, to);
    //Midpoint position
    LatLng p = computeOffset(from, distance * 0.5, heading);

    //Apply some mathematics to calculate position of the circle center
    double x = (1 - k * k) * distance * 0.5 / (2 * k);
    double r = (1 + k * k) * distance * 0.5 / (2 * k);

    LatLng c = computeOffset(p, x, heading + 90.0);

    //Polyline options
    List<LatLng> mapboxLatlng = new ArrayList<>();

    //Calculate heading between circle center and two points
    double h1 = computeHeading(c, from);
    double h2 = computeHeading(c, to);

    //Calculate positions of points on circle border and add them to polyline options
    int numpoints = 100;
    double step = (h2 - h1) / numpoints;

    for (int i = 0; i < numpoints; i++) {
        LatLng pi = computeOffset(c, r, h1 + i * step);
        mapboxLatlng.add(pi);
    }
    return mapboxLatlng;
}

static double computeHeading(LatLng from, LatLng to) {
    double fromLat = Math.toRadians(from.getLatitude());
    double fromLng = Math.toRadians(from.getLongitude());
    double toLat = Math.toRadians(to.getLatitude());
    double toLng = Math.toRadians(to.getLongitude());
    double dLng = toLng - fromLng;
    double heading = Math.atan2(Math.sin(dLng) * Math.cos(toLat), Math.cos(fromLat) * Math.sin(toLat) - Math.sin(fromLat) * Math.cos(toLat) * Math.cos(dLng));
    return wrap(Math.toDegrees(heading), -180.0D, 180.0D);
}

static double wrap(double n, double min, double max) {
    return n >= min && n < max ? n : mod(n - min, max - min) + min;
}

static double mod(double x, double m) {
    return (x % m + m) % m;
}

static LatLng computeOffset(LatLng from, double distance, double heading) {
    distance /= 6371009.0D;
    heading = Math.toRadians(heading);
    double fromLat = Math.toRadians(from.getLatitude());
    double fromLng = Math.toRadians(from.getLongitude());
    double cosDistance = Math.cos(distance);
    double sinDistance = Math.sin(distance);
    double sinFromLat = Math.sin(fromLat);
    double cosFromLat = Math.cos(fromLat);
    double sinLat = cosDistance * sinFromLat + sinDistance * cosFromLat * Math.cos(heading);
    double dLng = Math.atan2(sinDistance * cosFromLat * Math.sin(heading), cosDistance - sinFromLat * sinLat);
    return new LatLng(Math.toDegrees(Math.asin(sinLat)), Math.toDegrees(fromLng + dLng));
}

此代码将返回一个LatLng点列表,您可以在地图上绘制这些点,尽情享受吧!

This code will return a list of LatLng points that you can draw on your map, enjoy !

此代码产生的结果(当然不是模糊的部分!):

The result produced by this code (not the blurred part of course!):

欢迎对此代码进行任何改进!

Any improvement to this code is welcomed !