从swift中的另一个类获取位置结果

时间:2017-06-18 11:37:48

标签: ios swift geolocation mapkit cllocationmanager

如何从(userLocation.swift)的一个类(GPSLocation)获取位置结果,并在文件中的另一个类(target.swift)中使用它们。?

我需要countryCode,city,经度和纬度:

userLocation.swift

import UIKit
import MapKit

class GPSLocation {

func getGPSLocation(completion: () -> Void) {

    let locManager = CLLocationManager()
    var currentLocation: CLLocation!
    locManager.desiredAccuracy = kCLLocationAccuracyBest

    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {

        currentLocation = locManager.location

        let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
        let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
        let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)

        fetchCountryAndCity(location: location) { countryCode, city in
            // debugPrint("Country:", countryCode)
            // debugPrint("City:", city)
        }
        // debugPrint("Latitude:", latitude)
        // debugPrint("Longitude:", longitude)
    }
}

//MARK: Find countryCode & City name from longitude & latitude

func fetchCountryAndCity(location: CLLocation, completion: @escaping (String, String) -> ()) {
    CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
        if let error = error {
            debugPrint(error)
        } else if let countryCode = placemarks?.first?.isoCountryCode,
            let city = placemarks?.first?.locality {
            completion(countryCode, city)
        }
    }
}
}

并将其打印在文件中:

target.swift

import Foundation

class Post {

 fileprivate func Post() {

    func getLocation() {
        // Get user GPS location (longitude, latitude, Country Code, City)
        let getUserLocation = GPSLocation()
        getUserLocation.getGPSLocation {
            debugPrint("Country:", countryCode)
            debugPrint("City:", city)
            debugPrint("Latitude:", latitude)
            debugPrint("Longitude:", longitude)
        }
    }

  }

}

提前谢谢.. !!!

2 个答案:

答案 0 :(得分:5)

您可以定义在GPSLocation课程中获取位置后要运行的闭包。你可以通过两种方式实现它:

您可以在GPSLocation类中添加一个块代码变量,如下所示:

typealias completionHanlder = (_ lat: String, _ lng: String) -> Void
var completion: completionHanlder?

然后在实例化Post后的GPSLocation课程中,你可以传递一段代码,如:

getUserLocation.completion = {
       // do any stuff here     
}

OR 您可以将一段代码传递给getGPSLocation函数。以下是如何重新定义您的功能:

func getGPSLocation(completion: (_ lat: String, _ lng: String) -> Void) {        
    let locManager = CLLocationManager()
    var currentLocation: CLLocation!
    locManager.desiredAccuracy = kCLLocationAccuracyBest

    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {

    currentLocation = locManager.location

    let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
    let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
    let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)

    fetchCountryAndCity(location: location, completion: { countryCode, city in
        delegate?.fetchedLocationDetails(location: location, countryCode: countryCode, city: city)
    }) { delegate?.failedFetchingLocationDetails(error: $0) }

    debugPrint("Latitude:", latitude)
    debugPrint("Longitude:", longitude)

    completion(latitude, longitude)  // your block of code you passed to this function will run in this way

   }

}

这将是您的Post课程:

class Post {

func getLocation() {
    // Get user GPS location (longitude, latitude, Country Code, City)
    let getUserLocation = GPSLocation()
    getUserLocation.getGPSLocation { (lat, lng) in

        debugPrint("Latitude:", lat)
        debugPrint("Longitude:", lng)
        // HERE I NEED to PRINT longitude, latitude, Country Code, City
    }

}
}

当我在上面重写你的函数时,只要在getGPSLocation中调用完成块,就会运行这个闭包。

答案 1 :(得分:3)

您可以使用委托模式执行此操作。使用委托模式,您不仅可以获得经度,纬度,城市和国家,还可以进行错误处理。

首先,重写fetchCountryAndCity以包含错误处理程序:

func fetchCountryAndCity(location: CLLocation, completion: @escaping (String, String) -> (), errorHandler: @escaping (Error) -> ()) {
    CLGeocoder().reverseGeocodeLocation(location) { placemarks, error in
        if let error = error {
            debugPrint(error)
            errorHandler(error)
        } else if let countryCode = placemarks?.first?.isoCountryCode,
            let city = placemarks?.first?.locality {
            completion(countryCode, city)
        }
    }
}

然后,创建一个委托协议:

protocol GPSLocationDelegate {
    func fetchedLocationDetails(location: CLLocation, countryCode: String, city: String)
    func failedFetchingLocationDetails(error: Error)
}

将弱属性delegate添加到GPSLocation

weak var delegate: GPSLocationDelegate?

在适当的时候,调用委托方法:

public func getGPSLocation() {

    let locManager = CLLocationManager()
    var currentLocation: CLLocation!
    locManager.desiredAccuracy = kCLLocationAccuracyBest

    if (CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedWhenInUse || CLLocationManager.authorizationStatus() == CLAuthorizationStatus.authorizedAlways) {

        currentLocation = locManager.location

        let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
        let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
        let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)

        fetchCountryAndCity(location: location, completion: { countryCode, city in
            delegate?.fetchedLocationDetails(location: location, countryCode: countryCode, city: city)
        }) { delegate?.failedFetchingLocationDetails(error: $0) }

        debugPrint("Latitude:", latitude)
        debugPrint("Longitude:", longitude)
    }
}

Post课程中,让Post符合GPSLocationDelegate

class Post: GPSLocationDelegate {
    func fetchedLocationDetails(location: CLLocation, countryCode: String, city: String) {
         // print the stuff here
     }

     func failedFetchingLocationDetails(error: Error) {
         // handle errors
     }
}

添加名为gpsLocation的属性:

let gpsLocation = GPSLocation()

在初始化程序中,将其委托设置为self并致电getLocation

init() {
    gpsLocation.delegate = self
    gpsLocation.getLocation()
}

成功提取后,应打印您的位置详细信息。