2016-08-03 6 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; 
} 

[この質問をチェックしてください]このヘルプあなた

関連する問題