2017-06-18 58 views
2

如何从(userLocation.swift)中的一个类(GPSLocation)获取位置结果并将其用于文件(target.swift)中的另一个类中。从swift获取其他类的位置结果

我需要COUNTRYCODE,城市,经度和纬度:

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

    } 

} 

预先感谢您.. !!!

+0

创建共享实例/单身? – Larme

+0

当你调用'getGPSLocation'时,你不打印你想要的东西吗? – Sweeper

+0

将闭包传递给'getGPSLocation'并从'fetchCountryAndCity'完成处理函数中调用它 – Paulw11

回答

4

您可以在GPSLocation类中获取位置后定义闭包。

你可以有块码的变量在GPSLocation类象下面这样::您可以通过两种方式实现它实例GPSLocation

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

,那么你可以通过代码块像:

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函数调用完成块,我重写上面的功能。

+0

根据您的建议更新代码...但是我在收到错误时我想debugPrint在** target.swif ** ..我评论打印部分在** userLocation.swif ** t只是为了检查它是否正在工作... –

+0

好吧,我编辑了我的封闭,现在它有2个变量lat和lng,现在你可以在调用函数时在你的代码块中打印这些代码。 –

+1

不要忘记在getGPSLocation函数中调用完成块。 –

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() 
} 

您的位置信息应在成功读取后打印。

+0

我也试过这个,但它仅用于获取和打印** userLocation.swift **中的变量。我需要变量来打印另一个类Post,function Post()/ file ** target.swift ** –

+0

@FanbandTest请参阅已编辑的答案 – Sweeper