【问题标题】:Put latitude and longitude on a UILabel programmatically以编程方式将纬度和经度放在 UILabel 上
【发布时间】:2017-10-02 12:28:41
【问题描述】:

我似乎无法将纬度和经度显示在标签上。我已经尝试了很多东西,但我猜这可能很简单。

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
    }

所有视图都设置正确。

【问题讨论】:

    标签: ios swift mapkit core-location


    【解决方案1】:

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

    self.location = location
    

    【讨论】:

      【解决方案2】:

      使用这个

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

      【讨论】:

      • 不要使用描述。使用数字格式化程序或 String(format:) 初始化程序
      • 我尝试了 self.location = location 与 didUpdateLocations 并放入 latLabel.text = location.coordinate.latitude.description ,但它仍然没有改变任何东西?
      猜你喜欢
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 1970-01-01
      • 2018-09-09
      • 1970-01-01
      • 1970-01-01
      相关资源
      最近更新 更多