如何在标签上显示经度和纬度 - 快速

时间:2014-11-03 13:20:29

标签: swift

我正在尝试在名为经度和纬度的2个标签上显示经度和纬度,但我没有任何结果。

import UIKit
    import CoreLocation
    class ViewController: UIViewController, CLLocationManagerDelegate{

    @IBOutlet weak var longitude: UILabel!
    @IBOutlet weak var latitude: UILabel!
    let locationManager = CLLocationManager()
    override func viewDidLoad() {
            super.viewDidLoad()
            if (CLLocationManager.locationServicesEnabled()) {
                        locationManager.delegate = self
                        locationManager.desiredAccuracy = kCLLocationAccuracyBest
                        locationManager.requestWhenInUseAuthorization()
                        locationManager.startUpdatingLocation()
                    } else {
                        println("Location services are not enabled");
                    }
                }
        // MARK: - CoreLocation Delegate Methods
            func locationManager(manager: CLLocationManager!, didFailWithError error: NSError!) {
                locationManager.stopUpdatingLocation()
                removeLoadingView()
                if ((error) != nil) {
                    print(error)
                }
            }

            func locationManager(manager: CLLocationManager!, didUpdateLocations locations: [AnyObject]!) {
                var locationArray = locations as NSArray
                var locationObj = locationArray.lastObject as CLLocation
                var coord = locationObj.coordinate
                longitude.text = coord.longitude
                latitude.text = coord.latitude

            }
    }

我在longitude.text = coord.logitude上遇到错误,对于第二个表,它说它无法转换为字符串。我是Swift的新手。

1 个答案:

答案 0 :(得分:2)

您可以将数字转换为字符串,例如:

longitude.text = "\(coord.longitude)"