Swift 3.0从其他View Controller获取用户位置坐标 [英] Swift 3.0 Get User Location Coordinates from Different View Controller

查看:71
本文介绍了Swift 3.0从其他View Controller获取用户位置坐标的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

我有两个视图控制器-一个带有mapView的视图控制器,它可以通过locationManager获得用户位置的坐标,另一个是我希望能够提取这些用户坐标的VC.

I have two view controllers - one with the mapView that is able to obtain user location coordinations through locationManager, and a second VC that I wish to be able to pull these user coordinates.

第一个VC:MapView

First VC: MapView

func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
    var coordinatesOfUser = locations.last?.coordinate
    print("The value of usercoordinates are \(coordinatesOfUser)")

    // here I want to be able to pull this variable, coordinatesOfUser


    if let location = locations.last {
        let span = MKCoordinateSpanMake(0.00775, 0.00775)
        let myLocation = CLLocationCoordinate2DMake(location.coordinate.latitude,location.coordinate.longitude)
        let region = MKCoordinateRegionMake(myLocation, span)
        map.setRegion(region, animated: true)
    }
    self.map.showsUserLocation = true
    locationManager.stopUpdatingLocation()


}

第二个VC:

我当时正在考虑在此VC中调用locationManager函数.这是将坐标拉到该VC的最有效方法吗?如果是这样,我将如何去做呢?

I was thinking of calling the locationManager function in this VC. Is this the most efficient way to pull the coordinates to this VC? And if so, how would I go about doing it?

推荐答案

以下是解决此问题的几种方法:

Here's a couple options to solve this:

委托:您的secondVC可能有一个委托,该委托允许第一个视图控制器从中获取坐标.这样做的好处是,您可以收到进来的更新.

Delegation: Your secondVC could have a delegate that allows the first view controller to get coordinates from it. The advantage here is that you could receive updates as the come in.

protocol MyLocationDelegate: class {
    func newLocationArrived(location: CLLocation)
}

class FirstVC: UIViewController, MyLocationDelegate {

    override func viewDidLoad() {
        super.viewDidLoad()
    }

    func newLocationArrived(location: CLLocation) {
        print(location)
    }

    override func prepare(for segue: UIStoryboardSegue, sender: Any?) {
        if let dest = segue.destination as? SecondVC {
            dest.delegate = self
        }
    }
}

class SecondVC: UIViewController, CLLocationManagerDelegate {

    /// ... ...

    weak var delegate: MyLocationDelegate?

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

        /// do something with the location

        /// provide the data via delegation
        delegate?.newLocationArrived(location: CLLocation())
    }
}

通知:通过NSNotificationCenter发布通知.还可以接收传入的更新,只需通过通知中心而不是通过委托发送即可.

Notifications: Post a notification via NSNotificationCenter. Also able to receive updates as the come in, just send via notification center instead of through a delegate.

postNotificationName:object:userInfo:

postNotificationName:object:userInfo:

子视图控制器:根据第二个视图控制器及其视图是否是第一个的子视图,这可能允许直接访问.并非总是一种选择.

Child View Controller: Depending on whether the second view controller and its view are a child of the first, this could allow direct access. Not always an option.

Singleton(CLLocationManager):如果您打算在整个应用程序的其他位置使用位置服务,则可以使用Singleton将CLLocationManager移到其自己的类中.其他视图控制器可以根据其特定需求引用该类.在使用后台或重大更改位置时,这也可能会有所帮助,因为它们可能需要使用LaunchOptions键来重新启动位置管理器.

Singleton (CLLocationManager): If you plan to use Location Services in other places throughout the app, you can move the CLLocationManager into its own class with a Singleton. Other view controllers can reference that class for their specific needs. This can also be helpful when using the background or significant change locations as they might need to use the LaunchOptions Key to restart the location manager.

class MyLocationManager: CLLocationManager, CLLocationManagerDelegate {

    static let shared = MyLocationManager()

    var locations = [CLLocation]()

    func locationManager(_ manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
        for location in locations {
            self.locations.append(location)
        }
    }
}

这篇关于Swift 3.0从其他View Controller获取用户位置坐标的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆