Search code examples
iphoneobjective-ccompass-geolocation

Get geographical direction using magnetic heading


How to calculate magnetic heading value to get geographical direction i.e., North, North-East, East, East-South, South, South-West, West, West-North?

As magnetic heading returns only value of degree and its required for me to show the geographical direction, on the basis of continuous updated magnetic heading.

How can I make it possible?


Solution

  • Below is the code that I have used to display continuously updating geographical direction.

            CGFloat currentHeading = newHeading.magneticHeading;
            NSString *strDirection = [[NSString alloc] init];
    
            if(gradToRotate >23 && gradToRotate <= 67){
                  strDirection = @"NE";
            } else if(gradToRotate >68 && gradToRotate <= 112){
                  strDirection = @"E";
            } else if(gradToRotate >113 && gradToRotate <= 167){
                  strDirection = @"SE";
            } else if(gradToRotate >168 && gradToRotate <= 202){
                  strDirection = @"S";
            } else if(gradToRotate >203 && gradToRotate <= 247){
                  strDirection = @"SW";
            } else if(gradToRotate >248 && gradToRotate <= 293){
                  strDirection = @"W";
            } else if(gradToRotate >294 && gradToRotate <= 337){
                  strDirection = @"NW";
            } else if(gradToRotate >=338 || gradToRotate <= 22){
                  strDirection = @"N";
            }
    

    It is working fine at my end. Masters are welcome to let me know if any modification required.