【问题标题】:Printing current coordinates to a label (Swift, Xcode8)将当前坐标打印到标签(Swift,Xcode8)
【发布时间】:2017-02-13 18:18:07
【问题描述】:

我是初学者,我学习 Swift 只是为了好玩。现在我正在研究应用程序中地图的使用,我想将当前坐标(纬度,经度)打印到标签上。

现在,我已经设法在地图上显示地图和当前位置并获取它的坐标,但我需要帮助我如何将这些坐标打印到标签上。

这是我到目前为止所做的:

import UIKit
import MapKit
import CoreLocation

class ViewController: UIViewController, CLLocationManagerDelegate {

    @IBOutlet weak var posizione: UILabel!
    //Map
    @IBOutlet weak var map: MKMapView!



    @IBAction func rileva(_ sender: Any)
    {

    }


    let manager = CLLocationManager()

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

        let span:MKCoordinateSpan = MKCoordinateSpanMake(0.01, 0.01)
        let myLocation: CLLocationCoordinate2D = CLLocationCoordinate2DMake(location.coordinate.latitude, location.coordinate.longitude)
        let region:MKCoordinateRegion = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)

        print(location.coordinate)
        print(location.altitude)

        self.map.showsUserLocation = true

    }

【问题讨论】:

    标签: swift xcode8


    【解决方案1】:

    在您的 didUpdateLocation 函数中:

    self.posizione.text = "latitude: " + String(location.coordinate.latitude) + ", longitude: " + String(location.coordinate.longitude)
    

    【讨论】:

      【解决方案2】:

      你好试试这个代码:

      posizione.text = "latitude : \(location.coordinate.latitude) - longitude : \(location.coordinate.longitude)"
      

      【讨论】:

        【解决方案3】:

        使用。

        self.posizione.text = "Latitude is \(location.coordinate.latitute) and longitude is \(location.coordinate.longitude)".
        

        【讨论】:

        • 欢迎来到 Stack Overflow!虽然欢迎使用此代码 sn-p,并且可能会提供一些帮助,但它将是 greatly improved if it included an explanation如何为什么 这可以解决问题。请记住,您正在为将来的读者回答问题,而不仅仅是现在提问的人!请edit您的答案添加解释,并说明适用的限制和假设。
        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多