Get geographic direction using magnetic heading

How to calculate the value of the magnetic heading to get a geographical direction, that is, North, Northeast, East, East-South, South, Southwest, West, West-North?

Since the magnetic title returns only the degree value and it is necessary to display the geographical direction based on a continuously updated magnetic title.

How can I make this possible?

+6
source share
3 answers

Below is the code that I used to continuously update the geographic 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 works great on my part. Owners can let me know if any changes are required.

+9
source

Try this example suggested by Matt Neuburg on Github. https://github.com/mattneub/Programming-iOS-Book-Examples/blob/master/bk2ch22p775heading/ch35p1035heading/ViewController.swift

 func locationManager(manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) { var h = newHeading.magneticHeading let h2 = newHeading.trueHeading // will be -1 if we have no location info print("\(h) \(h2) ") if h2 >= 0 { h = h2 } let cards = ["N", "NE", "E", "SE", "S", "SW", "W", "NW"] var dir = "N" for (ix, card) in cards.enumerate() { if h < 45.0/2.0 + 45.0*Double(ix) { dir = card break } } print(dir) } 

It's fine!

+2
source
 CLLocationDirection heading = ((newHeading.trueHeading > 0) ? newHeading.trueHeading : newHeading.magneticHeading); NSString *geoDirectionString = [[NSString alloc] init]; if(heading >22.5 && heading <= 67.5){ geoDirectionString = @"North East"; } else if(heading >67.5 && heading <= 112.5){ geoDirectionString = @"East"; } else if(heading >112.5 && heading <= 157.5){ geoDirectionString = @"South East"; } else if(heading >157.5 && heading <= 202.5){ geoDirectionString = @"South"; } else if(heading >202.5 && heading <= 247.5){ geoDirectionString = @"South West"; } else if(heading >248 && heading <= 293){ geoDirectionString = @"West"; } else if(heading >247.5 && heading <= 337.5){ geoDirectionString = @"North West"; } else if(heading >=337.5 || heading <= 22.5){ geoDirectionString = @"North"; } 
+2
source

Source: https://habr.com/ru/post/891080/


All Articles