如何快速将图像旋转到特定的经纬度?

时间:2016-08-03 07:12:14

标签: ios swift core-location

我正试图将图像从地球的任何地方旋转到特定方向,例如箭头指向埃菲尔铁塔。我写了一个程序作为指南针。它表示东西南北方向。如何更新我的代码以找到埃菲尔铁塔(或任何其他固定的地方)方向。这是代码。

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

1 个答案:

答案 0 :(得分: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;
}

可能对此有所帮助