1

I am trying to rotate an image to particular direction from any where of the earth,for example an arrow sign to the eiffel tower. I have written a program which works as a compass. It denotes east west north south directions. How to update my code to find the eiffel tower(or any other fixed place) direction. Here is the code.

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)
 }
}
Bhumika
  • 876
  • 7
  • 20
Alex
  • 57
  • 1
  • 4
  • [Check this question](http://stackoverflow.com/questions/7173925/how-to-rotate-a-direction-arrow-to-particular-location) – Elena Aug 03 '16 at 10:09

1 Answers1

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

May be this help you

Sunil Sharma
  • 2,653
  • 1
  • 25
  • 36
Pavankumar
  • 288
  • 1
  • 10