我能够检索两点的度数并以该度数旋转图像。但为了显示方向,有时我需要在实际度数上加上 180,或者有时我不必加上度数。这是因为我正在使用自定义注释图像。任何形式的帮助将不胜感激。
- (double) getDirection
{
//Determine the direction
double lat1 = self.startCoOrdinate.latitude * M_PI / 180.0;
double lon1 = self.startCoOrdinate.longitude * M_PI / 180.0;
double lat2 = self.endCoOrdinate.latitude * M_PI / 180.0;
double lon2 = self.endCoOrdinate.longitude * M_PI / 180.0;
double dLon = lon2 - lon1;
double y = sin(dLon) * cos(lat2);
double x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
double radiansBearing = atan2(y, x);
NSLog(@"degre between points is %f",(radiansBearing * 180.0) / M_PI);
double degree = radiansBearing * 180.0 / M_PI;
return (degree + 0);
}
- (UIImage*)rotatedImage:(UIImage*)sourceImage byDegreesFromNorth:(double)degrees {
CGSize rotateSize = sourceImage.size;
UIGraphicsBeginImageContext(rotateSize);
CGContextRef context = UIGraphicsGetCurrentContext();
CGContextTranslateCTM(context, rotateSize.width/2, rotateSize.height/2);
CGContextRotateCTM(context, ( degrees * M_PI/180.0 ) );
CGContextScaleCTM(context, 1.0, -1.0);
CGContextDrawImage(UIGraphicsGetCurrentContext(),
CGRectMake(-rotateSize.width/2,-rotateSize.height/2,rotateSize.width, rotateSize.height),
sourceImage.CGImage);
UIImage *rotatedImage = UIGraphicsGetImageFromCurrentImageContext();
UIGraphicsEndImageContext();
return rotatedImage;
}