我可以获得Lat和Long,但我无法访问SWIFT中的GPS高度信息

时间:2015-01-05 12:21:16

标签: ios swift gps cllocation

我一直试图通过使用以下代码从CoreLocation框架获取的CLLocation来获取高度:

import UIKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

/*
Note: This needs to be added to the info.plist file for this to work:

<key>NSLocationUsageDescription</key> <string>Your message</string> <key>NSLocationAlwaysUsageDescription</key> <string>Your message</string> <key>NSLocationWhenInUsageDescription</key>
<string>Your message</string>
*/

@IBOutlet weak var gpsResult: UILabel!
@IBOutlet weak var altitudeLabel: UILabel!


var manager:CLLocationManager!

override func viewDidLoad() {
    super.viewDidLoad()
    // Do any additional setup after loading the view, typically from a nib.
    manager = CLLocationManager()
        manager.delegate = self
        manager.distanceFilter = kCLDistanceFilterNone
        manager.desiredAccuracy = kCLLocationAccuracyBest
        manager.requestAlwaysAuthorization()
        manager.startUpdatingLocation()

}

func locationManager(manager:CLLocationManager!, didUpdateLocations myLocations:CLLocation) {
    if manager != nil {
        var alt:CLLocationDistance = myLocations.altitude

        gpsResult.text = "locations = \(myLocations)"
        altitudeLabel.text = "GPS Altitude: \(Double(alt))"
        // manager.stopUpdatingLocation()
    }
}
}

因此,如果我只请求该位置,我能够获取gpsResult.text值并且它可以正常工作但是当我尝试访问高度时我收到错误:

 'NSInvalidArgumentException', reason: '-[__NSArrayM altitude]: unrecognized selector sent to instance 0x17404dcb0'

根据apple's reference,选择器应该存在。 我浏览了这里的帖子和网络,我尝试了他们的代码,但所有这些都失败了。

有没有人知道该怎么做?

感谢。

1 个答案:

答案 0 :(得分:0)

根据Apple的文档CLLocationManagerDelegatelocations的{​​{1}}参数给出了:

  

包含位置数据的CLLocation对象数组。这个   数组始终包含至少一个表示当前对象的对象   地点。如果延迟更新或多个位置到达   在交付之前,阵列可能包含其他内容   条目。数组中的对象按其中的顺序组织   他们发生了。因此,最近的位置更新是在   数组的结尾。

因此,您可以通过数组中的最后一个元素访问最近的位置:

didUpdateLocations