How to rotate a Direction Arrow to a particular location?

How to rotate a Direction Arrow to particular location

  • 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 point to that fix location so User will get direction to that location every time. I tried as follows. -(void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation { CLLocationCoordinate2D here = newLocation.coordinate; [self calculateUserAngle:here]; } -(void) calculateUserAngle:(CLLocationCoordinate2D)user { NSLog(@"my destination is %f ; %f", fixlocLat, fixlocLon); degrees = degrees + atan2(sin(fixlocLon-user.longitude)*cos(fixlocLat),cos(user.latitude)*sin(fixlocLat) - sin(user.latitude)*cos(fixlocLat)*cos(fixlocLon-user.longitude)); //get angle between user location and fix location } - (void)locationManager:(CLLocationManager *)manager didUpdateHeading:(CLHeading *)newHeading { arrow.transform = CGAffineTransformMakeRotation((degrees-newHeading.trueHeading) * M_PI / 180); } but the code above will always rotate arrow to approx. 2-3 degree north for every location. My question looks similar to http://stackoverflow.com/questions/5092845/point-to-location-using-compass.but using code from this link I am getting wrong direction. please help me on this. and also if any idea using accelerometer or gyrodata will be helpful for me. thanx in advance.

  • Answer:

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

DH14-S L at Stack Overflow Visit the source

Was this solution helpful to you?

Just Added Q & A:

Find solution

For every problem there is a solution! Proved by Solucija.

  • Got an issue and looking for advice?

  • Ask Solucija to search every corner of the Web for help.

  • Get workable solutions and helpful tips in a moment.

Just ask Solucija about an issue you face and immediately get a list of ready solutions, answers and tips from other Internet users. We always provide the most suitable and complete answer to your question at the top, along with a few good alternatives below.