2016-08-03 84 views
0

我想從地球的任何地方將圖像旋轉到特定的方向,例如箭頭標記到埃菲爾鐵塔。我寫了一個作爲指南針的程序。它表示東西南北方向。如何更新我的代碼以找到埃菲爾鐵塔(或任何其他固定的地方)的方向。這是代碼。如何快速將圖像旋轉到特定的經緯度?

class ViewController: UIViewController,CLLocationManagerDelegate { 

    var location: CLLocation! 
    var imgArrow: UIImageView! 
    var locationManager: CLLocationManager! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     imgArrow = UIImageView(image: UIImage(named: "arrow.png")) 
     self.view.addSubview(imgArrow) 

     if(!CLLocationManager.locationServicesEnabled()){ 
     return 
     } 
     locationManager = CLLocationManager() 
     locationManager.delegate = self 

    if(CLLocationManager.headingAvailable()){ 
     locationManager.startUpdatingHeading() 
    } 
    } 

override func viewDidAppear(animated: Bool) { 
    imgArrow.center = CGPointMake(CGRectGetMidX(self.view.bounds), CGRectGetMidY(self.view.bounds)) 
    } 
func locationManagerShouldDisplayHeadingCalibration(manager: CLLocationManager) -> Bool { 
    return true 
} 
func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { 
    let heading:CGFloat = CGFloat(-M_PI * (newHeading.magneticHeading + 90)/180.0) 


    imgArrow.transform = CGAffineTransformMakeRotation(heading) 
    print(heading) 
} 
} 
+0

[檢查這個問題(http://stackoverflow.com/questions/7173925/how-to-rotate-a-direction-arrow-to-特別是定位) – Elena

回答

0
#define degreesToRadians(x) (M_PI * x/180.0) 
#define radiandsToDegrees(x) (x * 180.0/M_PI) 
- (float)getHeadingForDirectionFromCoordinate:(CLLocationCoordinate2D)fromLoc toCoordinate:(CLLocationCoordinate2D)toLoc{ 
    float fLat = degreesToRadians(fromLoc.latitude); 
    float fLng = degreesToRadians(fromLoc.longitude); 
    float tLat = degreesToRadians(toLoc.latitude); 
    float tLng = degreesToRadians(toLoc.longitude); 

    float degree = atan2(sin(tLng-fLng)*cos(tLat), cos(fLat)*sin(tLat)-sin(fLat)*cos(tLat)*cos(tLng-fLng)); 

    float deg = radiandsToDegrees(degree); 
    NSLog(@"%f",deg); 

    return degreesToRadians(deg); 
} 
-(MKAnnotationView *)mapView:(MKMapView *)mapView viewForAnnotation:(id<MKAnnotation>)annotation { 
    MKAnnotationView *annotationView = [[MKAnnotationView alloc]init]; 
     annotationView.image = [UIImage imageNamed:@"ainted_sportscar___top_view_by_balagehun1991.png"]; 
     annotationView.bounds = CGRectMake(0, 0, 10, 22.5); 
     annotationView.transform = CGAffineTransformRotate(mapView.transform, return angle of function); 
    return annotatio.View; 
} 

可能是這個幫助你