查找 swift 中 iPhone 的罗盘方向
Finding the compass orientation of an iPhone in swift
我正在尝试制作一个类似于指南针的应用程序,但没有table 差异,但是,它们并不重要。我需要知道的是:无论 phone 的方向如何,我如何才能让 iPhone 给我罗盘方向(即北 0 度)它平躺在 table 或某人手中的肖像上,如果它指向相同的方向)
TL;DR 如何获得 iPhone 围绕 y 轴的旋转,大约每秒更新一次。
import CoreLocation
class ViewController: UIViewController ,CLLocationManagerDelegate {
var lm:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
lm = CLLocationManager()
lm.delegate = self
lm.startUpdatingHeading()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {
println(newHeading.magneticHeading)
}
}
您可以从https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLHeading_Class/
获取更多信息
Swift 3:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Azimuth
if (CLLocationManager.headingAvailable()) {
locationManager.headingFilter = 1
locationManager.startUpdatingHeading()
locationManager.delegate = self
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
print (heading.magneticHeading)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 3.0
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var compass: UIImageView!
@IBOutlet weak var angleLabel: UILabel!
@IBOutlet weak var geographicalDirectionLabel: UILabel!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
// Start location services to get the true heading.
locationManager.distanceFilter = 1000
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.startUpdatingLocation()
//Start heading updating.
if CLLocationManager.headingAvailable() {
locationManager.headingFilter = 5
locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
if newHeading.headingAccuracy < 0 {
return
}
// Get the heading(direction)
let heading: CLLocationDirection = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);
UIView.animate(withDuration: 0.5) {
let angle = CGFloat(heading).toRadians // convert from degrees to radians
self.compass.transform = CGAffineTransform(rotationAngle: angle) // rotate the picture
}
print(heading)
angleLabel.text = String(format: "%0.2f", heading)
var strDirection = String()
if(heading > 23 && heading <= 67){
strDirection = "North East";
} else if(heading > 68 && heading <= 112){
strDirection = "East";
} else if(heading > 113 && heading <= 167){
strDirection = "South East";
} else if(heading > 168 && heading <= 202){
strDirection = "South";
} else if(heading > 203 && heading <= 247){
strDirection = "South West";
} else if(heading > 248 && heading <= 293){
strDirection = "West";
} else if(heading > 294 && heading <= 337){
strDirection = "North West";
} else if(heading >= 338 || heading <= 22){
strDirection = "North";
}
geographicalDirectionLabel.text = strDirection
}
}
我正在尝试制作一个类似于指南针的应用程序,但没有table 差异,但是,它们并不重要。我需要知道的是:无论 phone 的方向如何,我如何才能让 iPhone 给我罗盘方向(即北 0 度)它平躺在 table 或某人手中的肖像上,如果它指向相同的方向)
TL;DR 如何获得 iPhone 围绕 y 轴的旋转,大约每秒更新一次。
import CoreLocation
class ViewController: UIViewController ,CLLocationManagerDelegate {
var lm:CLLocationManager!
override func viewDidLoad() {
super.viewDidLoad()
lm = CLLocationManager()
lm.delegate = self
lm.startUpdatingHeading()
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
}
func locationManager(manager: CLLocationManager!, didUpdateHeading newHeading: CLHeading!) {
println(newHeading.magneticHeading)
}
}
您可以从https://developer.apple.com/library/ios/documentation/CoreLocation/Reference/CLHeading_Class/
获取更多信息Swift 3:
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
let locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
// Azimuth
if (CLLocationManager.headingAvailable()) {
locationManager.headingFilter = 1
locationManager.startUpdatingHeading()
locationManager.delegate = self
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading heading: CLHeading) {
print (heading.magneticHeading)
}
override func didReceiveMemoryWarning() {
super.didReceiveMemoryWarning()
// Dispose of any resources that can be recreated.
}
}
Swift 3.0
import UIKit
import CoreLocation
class ViewController: UIViewController, CLLocationManagerDelegate {
@IBOutlet weak var compass: UIImageView!
@IBOutlet weak var angleLabel: UILabel!
@IBOutlet weak var geographicalDirectionLabel: UILabel!
var locationManager = CLLocationManager()
override func viewDidLoad() {
super.viewDidLoad()
locationManager.delegate = self
// Start location services to get the true heading.
locationManager.distanceFilter = 1000
locationManager.desiredAccuracy = kCLLocationAccuracyKilometer
locationManager.startUpdatingLocation()
//Start heading updating.
if CLLocationManager.headingAvailable() {
locationManager.headingFilter = 5
locationManager.startUpdatingHeading()
}
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
if newHeading.headingAccuracy < 0 {
return
}
// Get the heading(direction)
let heading: CLLocationDirection = ((newHeading.trueHeading > 0) ?
newHeading.trueHeading : newHeading.magneticHeading);
UIView.animate(withDuration: 0.5) {
let angle = CGFloat(heading).toRadians // convert from degrees to radians
self.compass.transform = CGAffineTransform(rotationAngle: angle) // rotate the picture
}
print(heading)
angleLabel.text = String(format: "%0.2f", heading)
var strDirection = String()
if(heading > 23 && heading <= 67){
strDirection = "North East";
} else if(heading > 68 && heading <= 112){
strDirection = "East";
} else if(heading > 113 && heading <= 167){
strDirection = "South East";
} else if(heading > 168 && heading <= 202){
strDirection = "South";
} else if(heading > 203 && heading <= 247){
strDirection = "South West";
} else if(heading > 248 && heading <= 293){
strDirection = "West";
} else if(heading > 294 && heading <= 337){
strDirection = "North West";
} else if(heading >= 338 || heading <= 22){
strDirection = "North";
}
geographicalDirectionLabel.text = strDirection
}
}