How to rotate a Direction Arrow to particular location

前端 未结 4 469
谎友^
谎友^ 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:09

    this code may help you

    -(void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading {
        // Use the true heading if it is valid. 
        CLLocationDirection direction = newHeading.magneticHeading;
        CGFloat radians = -direction / 180.0 * M_PI;
    
        self.strAccuracy = [NSString stringWithFormat:@"%.1fmi",newHeading.headingAccuracy];    
        [lblAccuracy setText:self.strAccuracy];
    
        //Rotate Bearing View
        [self rotateBearingView:bearingView radians:radians];
    
        //For Rotate Niddle
        CGFloat angle = RadiansToDegrees(radians);  
        [self setLatLonForDistanceAndAngle];
        [self rotateArrowView:arrowView degrees:(angle + fltAngle)];
    }
    
    -(void)rotateArrowView:(UIView *)view degrees:(CGFloat)degrees
    {
        CGAffineTransform transform = CGAffineTransformMakeRotation(DegreesToRadians(degrees));
        view.transform = transform;
    }
    
    -(void)setLatLonForDistanceAndAngle
    {
        dblLat1 = DegreesToRadians(appDelegate.dblLatitude);
        dblLon1 = DegreesToRadians(appDelegate.dblLongitude);
    
        dblLat2 = DegreesToRadians(objClsProductSearch.dblLatitude);
        dblLon2 = DegreesToRadians(objClsProductSearch.dblLongitude);
    
        fltLat = dblLat2 - dblLat1;
        fltLon = dblLon2 - dblLon1;
    }
    
    -(float)getAngleFromLatLon
    {
        //Calculate angle between two points taken from http://www.movable-type.co.uk/scripts    /latlong.html
        double y = sin(fltLon) * cos(dblLat2);
        double x = cos(dblLat1) * sin(dblLat2) - sin(dblLat1) * cos(dblLat2) * cos(dblLon2);
        CGFloat angle = RadiansToDegrees(atan2(y, x));
        return angle;
    }
    

提交回复
热议问题