Найдите среднюю точку между двумя значениями CLLocationCoordinate2D при повороте на 180 градусов
У меня есть следующий код для разработки MKCoordinateRegion для отображения карты, включающей две определенные точки. Проблема в том, что он не работает, когда долгота превышает 180 или -180 градусов.
CLLocationCoordinate2D tempPoint1 = CLLocationCoordinate2DMake(startLat,startLong);
CLLocationCoordinate2D tempPoint2 = CLLocationCoordinate2DMake(nextLat,nextLong);
if (lineType == 1) {
[self createGreatCircleMKPolylineFromPoint: tempPoint1 toPoint: tempPoint2 forMapView:mapView];
}
else {
CLLocationCoordinate2D *coords = malloc(sizeof(CLLocationCoordinate2D) * 2);
coords[0] = tempPoint1;
coords[1] = tempPoint2;
MKPolyline *polyline = [MKPolyline polylineWithCoordinates:coords count:2];
free(coords);
[mapView addOverlay:polyline];
}
double lon1 = tempPoint1.longitude * M_PI / 180;
double lon2 = tempPoint2.longitude * M_PI / 180;
double lat1 = tempPoint1.latitude * M_PI / 180;
double lat2 = tempPoint2.latitude * M_PI / 180;
double dLon = lon2 - lon1;
double x = cos(lat2) * cos(dLon);
double y = cos(lat2) * sin(dLon);
double lat3 = atan2( sin(lat1) + sin(lat2), sqrt((cos(lat1) + x) * (cos(lat1) + x) + y * y) );
double lon3 = lon1 + atan2(y, cos(lat1) + x);
CLLocationCoordinate2D center;
center.latitude = lat3 * 180 / M_PI;
center.longitude = lon3 * 180 / M_PI;
MKCoordinateSpan locationSpan;
locationSpan.latitudeDelta = fabs(tempPoint1.latitude - tempPoint2.latitude) * 1.2;
locationSpan.longitudeDelta = fabs(tempPoint1.longitude - tempPoint2.longitude) * 1.2;
MKCoordinateRegion region = {center, locationSpan};
[mapView setRegion:region];
Любые идеи, как я мог бы изменить этот код, чтобы убедиться, что он может обрабатывать startLong положительным, т. Е. 174 и nextLong отрицательным, т. Е. -127, и наоборот
1 ответ
Как startLong
а также nextLong
долготы в градусах, поэтому от 0° до 360°, вы можете убедиться, что они оба положительные и nextLong
> = startLong
:
while( startLong < 0.0 ) startLong += 360.0f;
while( startLong >= 360.0 ) startLong -= 360.0f;
while( nextLong < startLong ) nextLong += 360.0f;
while( nextLong - startLong > 360.0 ) nextLong -= 360.0;
При этом вы уверены, что:
startLong
от 0,0 до 360,0 (исключая позднее)nextLong
находится междуstartLong
а такжеstartLong
+ 360,0