1
2 개의 화살표가 2 개의 이미지로 표시됩니다. 북쪽은 항상 북쪽을 가리키고 있지만 도시 화살표는 고정되지 않습니다. 장치를 움직이지 않아도 계속 변동합니다. 이 문제를 해결하려면 어떻게해야합니까?Swift를 사용하여 도시를 가리키는 나침반을 만들려고 시도합니다. 3
또한 오류가 timeToGetTheDirection 함수에 있다고 의심됩니다.
import UIKit
import CoreLocation
struct TokyoLocationConstants {
static let cityLat = 35.6895
static let cityLong = 139.6917
}
class ViewController: UIViewController, CLLocationManagerDelegate {
let obj1 = CLLocationManager()
var degrees: Double = 0
override func viewDidLoad() {
obj1.startUpdatingLocation()
obj1.startUpdatingHeading()
}
func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
timeToGetTheDirection(location: locations.last!)
}
func timeToGetTheDirection(location: CLLocation) {
let lat1 = location.coordinate.latitude // My current Latitude
let long1 = location.coordinate.longitude // My current Longitude
// Fromula to get the direction to a city from this site:
// http://www.movable-type.co.uk/scripts/latlong.html
let differenceInLong = TokyoLocationConstants.cityLong - long1
let y = sin(differenceInLong) * cos(TokyoLocationConstants.cityLat)
let x = cos(lat1) * sin(TokyoLocationConstants.cityLat) - sin(lat1) * cos(TokyoLocationConstants.cityLat) * cos(differenceInLong)
let theAngleInRad = atan2(y, x)
var theAngleInDeg = theAngleInRad * (180/M_PI)
if(theAngleInDeg < 0){
theAngleInDeg = -theAngleInDeg;
} // End of Formulla
degrees = theAngleInDeg; // Assign the result of formula above to global var to be used below
}
func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
var rad: Double = 0
if newHeading.trueHeading > 0 {
// This will make the 1st arrow points to the North
rad = newHeading.trueHeading * (M_PI/180)
imageView.transform = CGAffineTransform(rotationAngle: CGFloat(-rad))
}
// This will make the 2nd arrow points to the City
let cityDegreeInRad = (M_PI/180) * degrees
let something = (newHeading.trueHeading - (cityDegreeInRad))
lineImageView.transform = CGAffineTransform(rotationAngle: CGFloat(-something))
}