2017-02-13 58 views
0

我是初學者,我正在學習斯威夫特只是爲了好玩。現在我正在研究在應用程序中使用地圖,我想將當前座標(緯度,經度)打印到標籤上。現在打印當前座標到標籤(斯威夫特,Xcode8)

,我已經成功地顯示在地圖和地圖上的當前位置,並獲得它的座標,但我需要幫助,我怎麼能打印這些座標的標籤。

這是我迄今所做的:

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 

    } 

回答

0

內,您的didUpdateLocation功能:

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

喜試試這個代碼:

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

使用。

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

歡迎堆棧溢出!雖然這段代碼片段是受歡迎的,並且可能會提供一些幫助,但如果它包含* how *和* why *的解釋](// meta.stackexchange.com/q/114762),它會[大大改進],這將解決問題。請記住,你正在爲將來的讀者回答這個問題,而不僅僅是現在問的人!請編輯您的答案以添加解釋,並指出適用的限制和假設。 –