问题
I want to draw a curved polyline between two points on a map with the Mapbox SDK.
I could not find any solution from the Mapbox SDK. The Turf library is not ready yet to use it on Android.
回答1:
I found a solution with the google maps sdk so I converted it to use only the Mapbox sdk :
Inspired by Can I draw a curved dashed line in 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));
}
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 !
来源:https://stackoverflow.com/questions/56704718/how-to-draw-curved-polyline-in-mapbox-sdk-for-android