我有以下代码来制定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];
我如何更改此代码的任何想法以确保它可以处理启动是积极的,即174,而隔壁是负面的,即-127,反之亦然
as 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
之间