ios - 将纬度和经度坐标传递到第二个 View Controller

标签 ios swift mapkit coordinates

我希望将纬度和经度坐标保存在第二个 View Controller 上的两个标签中,但我无法使其正常工作。

这是我的代码:

第一个 View Controller :

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)

    }    

}

第二个 View Controller :

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.
    }

}

最佳答案

当你推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)

关于ios - 将纬度和经度坐标传递到第二个 View Controller ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/44285091/

相关文章:

ios - 将静态参数添加到 Swift 中的#selector

ios - View Controller 不符合协议(protocol) AVCaptureFileOutputRecordingDelegate

ios - mapView.delegate = self 与当前位置

ios - Firebase 数据库不离线返回值

objective-c - 如何在 UIViewController 中创建 View 容器?

ios - 如何为自定义 uicontrol 和 controller 添加触摸事件?

ios - UIView.animate View 框架的问题

ios - xcode 中的数据库在多个位置使用?

ios - 在 iOS 6 中以编程方式打开 map 应用

ios - Objective-C/ALAssetLibrary - 如何读取和保存图像信息