2017-05-31 57 views
0

我希望將緯度和經度座標保存在第二個視圖控制器上的兩個標籤中,但我無法使其工作。將經緯度座標傳入第二個視圖控制器

這裏是我的代碼:

第一個視圖控制器:

import UIKit 
import CoreLocation 
import MapKit 

class ViewController: UIViewController, CLLocationManagerDelegate, MKMapViewDelegate { 

    @IBOutlet var map: MKMapView! 

    var locationManager = CLLocationManager() 

    override func viewDidLoad() { 
     super.viewDidLoad() 
     // Do any additional setup after loading the view, typically from a nib. 

     locationManager.delegate = self 
     locationManager.desiredAccuracy = kCLLocationAccuracyBest 
     locationManager.requestWhenInUseAuthorization() 
     locationManager.startUpdatingLocation() 

    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) { 

     let userLocation: CLLocation = locations[0] 

     let latitude = userLocation.coordinate.latitude 

     let longitude = userLocation.coordinate.longitude 

     let latDelta: CLLocationDegrees = 0.05 

     let lonDelta: CLLocationDegrees = 0.05 

     let span = MKCoordinateSpan(latitudeDelta: latDelta, longitudeDelta: lonDelta) 

     let location = CLLocationCoordinate2D(latitude: latitude, longitude: longitude) 

     let region = MKCoordinateRegion(center: location, span: span) 

     self.map.setRegion(region, animated: true) 

    }  

} 

第二個視圖控制器:

import UIKit 

class AddSightingViewController: UIViewController { 

    var getCoordinates: ViewController! 

    @IBOutlet weak var latitudeLabel: UILabel! 
    @IBOutlet weak var longitudeLabel: UILabel! 

    override func viewDidLoad() { 
     super.viewDidLoad() 

     // Do any additional setup after loading the view. 

     latitudeLabel.text = "\(getCoordinates.locationManager.location?.coordinate.latitude)" 
     longitudeLabel.text = "\(getCoordinates.locationManager.location?.coordinate.longitude)" 


    } 

    override func didReceiveMemoryWarning() { 
     super.didReceiveMemoryWarning() 
     // Dispose of any resources that can be recreated. 
    } 

} 
+0

你能否提供更多關於「無法正常工作」的細節。什麼不起作用?你有什麼嘗試?是否有代碼設置'AddSightingViewController.getCoordinates'屬性的值? –

+0

對不起@KristopherJohnson我的代碼要麼產生0.0的座標,要麼對於經度和長度都是0,要麼發現零錯誤,上面的代碼 – Elfuthark

+1

創建一個singleton類來獲取位置並從那裏獲取值到任何視圖控制器 –

回答

0

當你把VC

objAddSightVC.location = CLLocationCoordinate2D(latitude: your_lat, longitude: your_long) 

AddSightingViewController

var location : CLLocationCoordinate2D! 

latitudeLabel.text = String(describing: location.latitude) 
longitudeLabel.text = String(describing: location.longitude) 
0
在故事板

發起或AddSightingViewController推動VC

let coordinate = CLLocationCoordinate2D(latitude: lat, longitude: long) 
let addSightingViewController = AddSightingViewController(location :coordinate) 

使用init構造財產申報位置

class AddSightingViewController: UIViewController { 

    let location:LLocationCoordinate2D 

    init(location:LLocationCoordinate2D!){ 

    self.location = location 
} 

override func viewDidLoad() { 
     super.viewDidLoad() 

     latitudeLabel.text = "\(location?.coordinate.latitude)" 
     longitudeLabel.text = "\(location?.coordinate.longitude)" 

} 

} 
相關問題