мы используем одну и ту же функцию в iOS и Android, одни и те же входные данные, и вывод отличается от второго / третьего десятичного числа.
Я могу понять причину, и нам действительно нужно быть точно такими же.
Следуя функции iOS:
func angleBoundSetFromPosition(userPosition: CLLocationCoordinate2D, distance: Double, bearing: Double) -> (CLLocationCoordinate2D) {
let dist = Float(distance) / Float(6371)
let brg = Float(bearing.degreesToRadians)
let lat1 = Float(userPosition.latitude).degreesToRadians
let lon1 = Float(userPosition.longitude).degreesToRadians
let lat2 = asin(sin(lat1) * cos(dist) + cos(lat1) * sin(dist) * cos(brg))
let lon2 = lon1 + atan2(sin(brg) * sin(dist) * cos(lat1), cos(dist) - sin(lat1) * sin(lat2))
print("for: \(bearing), dist: \(dist), bearing: \(brg), lat1: \(lat1), lon1: \(lon1), lat2: \(lat2), lon2: \(lon2)")
return CLLocationCoordinate2D(latitude: Double(lat2.radiansToDegrees), longitude: Double(lon2.radiansToDegrees))
}
Расширения iOS:
extension FloatingPoint {
var degreesToRadians: Self { return self / 180 * .pi}
var radiansToDegrees: Self { return self * 180 / .pi }
}
А вот и ява:
public static LatLng getDestinationPoint(LatLng source, double brng, double dist) {
dist = dist / 6371;
brng = Math.toRadians(brng);
double lat1 = Math.toRadians(source.latitude), lon1 = Math.toRadians(source.longitude);
double lat2 = Math.asin(Math.sin(lat1) * Math.cos(dist) +
Math.cos(lat1) * Math.sin(dist) * Math.cos(brng));
double lon2 = lon1 + Math.atan2(Math.sin(brng) * Math.sin(dist) *
Math.cos(lat1),
Math.cos(dist) - Math.sin(lat1) *
Math.sin(lat2));
if (Double.isNaN(lat2) || Double.isNaN(lon2)) {
return null;
}
return new LatLng(Math.toDegrees(lat2), Math.toDegrees(lon2));
}
Ввод:
UserPosition(45.6091463, 8.8496677)
distance = 10
called once with bearing = -45, and once with bearing 135
Выход:
Android
{latitude 45.621861885764005, longitude 8.83148183051733}
{latitude 45.596425230386295, longitude 8.867843324603944}
IOS
{latitude 45.67270183849526, longitude 8.758660791031469}
{latitude 45.54551866514264, longitude 8.940468486905017}