How to rotate a Direction Arrow to particular location

前端 未结 4 482
谎友^
谎友^ 2020-12-02 19:32

In my app I have a latitude-longitude of 1 fix location. Now user with iPhone device can move anywhere and even he rotate his device, the arrow (some uiimageview) should poi

4条回答
  •  暖寄归人
    2020-12-02 20:06

    -(void)viewDidLoad
    {
      locationManager = [[CLLocationManager alloc] init];
      locationManager.delegate = self;
      locationManager.desiredAccuracy = kCLLocationAccuracyBest;
      locationManager.distanceFilter = kCLDistanceFilterNone;
      [locationManager startUpdatingLocation];
      [locationManager startUpdatingHeading];
    }
    -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
    {
      GeoAngle = [self setLatLonForDistanceAndAngle:newLocation];
    }
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateLocations:(NSArray *)locations
    {
       CLLocation *location=[locations lastObject];
       GeoAngle = [self setLatLonForDistanceAndAngle:location];
    }
    -(float)setLatLonForDistanceAndAngle:(CLLocation *)userlocation
    {
       float lat1 = DegreesToRadians(userlocation.coordinate.latitude);
       float lon1 = DegreesToRadians(userlocation.coordinate.longitude);
    
       float lat2 = DegreesToRadians(Destination lattitude);
       float lon2 = DegreesToRadians(Destination Longitude);
    
       float dLon = lon2 - lon1;
    
       float y = sin(dLon) * cos(lat2);
       float x = cos(lat1) * sin(lat2) - sin(lat1) * cos(lat2) * cos(dLon);
       float radiansBearing = atan2(y, x);
       if(radiansBearing < 0.0)
       {
          radiansBearing += 2*M_PI;
       }
       return radiansBearing;
    }
    
    -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading
    {
       float direction = -newHeading.trueHeading;
       arrow.transform=CGAffineTransformMakeRotation((direction* M_PI / 180)+ GeoAngle);
    }
    

提交回复
热议问题