【问题标题】:Trying to build a compass that points to a city using Swift 3尝试使用 Swift 3 构建指向城市的指南针
【发布时间】:2017-03-24 19:31:06
【问题描述】:

我有 2 个箭头作为 2 个图像,指向北方的那个总是指向北方,但城市箭头永远不会保持固定。即使我不移动设备,它也会不断波动。我应该怎么做才能解决这个问题?

另外,我怀疑错误出在 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))
}

【问题讨论】:

    标签: swift core-location cllocationmanager cllocation


    【解决方案1】:

    试试这个

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    
                        let location = CLLocation()
    
                        let userLoc : CLLocationCoordinate2D = location.coordinate
                        self.calUserAngle(here: userLoc)
    
                    }
    
    func calUserAngle(here: CLLocationCoordinate2D) {
    
                    var x = 0.0
                    var y = 0.0
                    var delLon = 0.0
    
                    //Tokio Coords
                    let tokioLat = 35.6895
                    let tokioLon = 139.6917
    
                    delLon = tokioLon - here.longitude
                    y = sin(delLon) * cos(tokioLat);
                    x = cos(here.latitude) * sin(tokioLat) - sin(here.latitude) * cos(tokioLat) * cos(delLon)
    
                    let theAngleInRad = atan2(y, x)
                    var theAngleInDeg = theAngleInRad * (180 / .pi)
    
                    if(theAngleInDeg < 0){
                        theAngleInDeg = -theAngleInDeg
                    } else {
                        theAngleInDeg = 360 - theAngleInDeg
                    }
    
                    //degress is my Double var
                    degress = theAngleInDeg
                }
    
            func locationManager(_ manager: CLLocationManager, didUpdateHeading newHeading: CLHeading) {
    
                    print(String(format: "%.0f°", newHeading.magneticHeading))
    
                    //Convert Degree to Radian and move the needle
                    let newRad =  -newHeading.trueHeading * Double.pi/180
    
                    //First arrow animation to destination
                    UIView.animate(withDuration:0.3){
                        self.imgArrowUP.transform = CGAffineTransform(rotationAngle: CGFloat((self.degress - newHeading.trueHeading) * .pi/180));
                    }
    
                    //Second arrow animation to North
                    UIView.animate(withDuration:0.6){
                        self.compassView.transform = CGAffineTransform(rotationAngle: CGFloat(newRad));
                    }
    
                }
    

    希望对你有所帮助

    【讨论】:

      猜你喜欢
      • 2016-05-15
      • 2011-07-02
      • 2014-02-15
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2016-05-05
      • 1970-01-01
      相关资源
      最近更新 更多