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

标签 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
    }

所有 View 均已正确设置。

最佳答案

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

self.location = location

关于ios - 以编程方式将纬度和经度放在 UILabel 上,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/46525470/

相关文章:

ios - 无法使用 '[NSObject : AnyObject]' 类型的索引为 'String' 类型的值添加下标

Swift CLLocationManager(didUpdateLocations) 调用过于频繁

android - 从 iOS 应用调用时, native Web RTC 视频通话在 Android 上卡住

ios - 如何添加动态部分但仍然只添加到其中一个部分?

ios - 已弃用 : FCM direct channel is deprecated, 请使用 APNs 进行下游消息处理

ios - 当用户停止使用 swift 5 说话时如何终止语音识别

ios - 使用swift和Mapkit,显示用户当前位置到特定位置之间的路线

iphone - 如何根据时间范围创建新的日历事件?

javascript - React Native Await Async 在提醒用户和设置状态之前不返回值

json - 如何在 Swift 中将嵌套的 JSON 字符串解析为对象?