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

Posted

技术标签:

【中文标题】以编程方式将纬度和经度放在 UILabel 上【英文标题】:Put latitude and longitude on a UILabel programmatically 【发布时间】: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
    

所有视图都设置正确。

【问题讨论】:

【参考方案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 ,但它仍然没有改变任何东西?

以上是关于以编程方式将纬度和经度放在 UILabel 上的主要内容,如果未能解决你的问题,请参考以下文章

给定每个区域的边界和纬度/经度,以编程方式查找州/县

R或Java:是不是有将邮政编码转换为纬度和经度并放在地图上的包?

如何以编程方式在 Android 的 API 23 及更高版本中获取位置(纬度、经度)?

是否有从 Thomas Bros 地图页面和网格转换为纬度/经度的公式?

你在哪里以编程方式在 UIView 中为 UILabel 设置文本?

显示经度/纬度点的子集?