2016-08-03 15 views
0

Ich versuche ein Bild von jedem Ort der Erde in eine bestimmte Richtung zu drehen, zum Beispiel ein Pfeilzeichen zum Eiffelturm. Ich habe ein Programm geschrieben, das als Kompass funktioniert. Es bezeichnet Ost-West-Nord-Süd-Richtungen. Wie aktualisiere ich meinen Code, um die Richtung des Eiffelturms (oder eines anderen festen Ortes) zu finden. Hier ist der Code.Wie man das Bild zu einem bestimmten Breiten- und Längengrad in Swift dreht?

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

[diese Frage prüfen] (http://stackoverflow.com/questions/7173925/how-to-rotate-a-direction-arrow-to- besondere Lage) – Elena

Antwort

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; 
} 

Mai dieses helfen Sie