以编程方式将纬度和经度放在UILabel上

时间:2017-10-02 12:28:42

标签: ios swift mapkit core-location

我似乎无法在标签上显示经度和经度。我尝试过很多东西,虽然我猜它可能很简单。

import Foundation
import UIKit
import CoreLocation
import MapKit  

class LocationVC: UIViewController, CLLocationManagerDelegate {
    var location: CLLocation! {
        didSet{
            longLabel.text = ("\(location.coordinate.latitude)")

            latLabel.text  = ("\(location.coordinate.longitude)")
        }
    }

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation])
    {
        let location = locations[0]

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.5, 0.5)
        let myLocation:CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)

        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        print("hhhhh\(location.coordinate.latitude)")

        print(location.altitude)
        print(location.speed)

        self.map.showsUserLocation = true
    }

所有视图都已正确设置。

2 个答案:

答案 0 :(得分:3)

您没有更改类的location变量的值,这会触发标签的文本更改。将其附加到didUpdateLocations

self.location = location

答案 1 :(得分:0)

使用此

longLabel.text = location.coordinate.latitude.description
latLabel.text  = location.coordinate.longitude.description