热门标签 | HotTags
当前位置:  开发笔记 > 编程语言 > 正文

快速获取其他班级的定位结果

如何解决《快速获取其他班级的定位结果》经验,为你挑选了1个好方法。

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

我需要以下国家/地区的国家/地区代码,城市,经度和纬度:

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

  }

}

先感谢您..!!!



1> Maryam Fekri..:

您可以定义一个闭包,以在获取您的GPSLocation类的位置后运行。您可以通过两种方式实现它:

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

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

然后在Post实例化后的类中GPSLocation可以传递如下代码块:

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

或者,您可以将代码块传递给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当我在上面重写您的函数时,只要在函数中调用了完成块,您放置在此处的闭包就会运行。


推荐阅读
author-avatar
爲妳奮鬥壹輩子丶故事_932
这个家伙很懒,什么也没留下!
PHP1.CN | 中国最专业的PHP中文社区 | DevBox开发工具箱 | json解析格式化 |PHP资讯 | PHP教程 | 数据库技术 | 服务器技术 | 前端开发技术 | PHP框架 | 开发工具 | 在线工具
Copyright © 1998 - 2020 PHP1.CN. All Rights Reserved | 京公网安备 11010802041100号 | 京ICP备19059560号-4 | PHP1.CN 第一PHP社区 版权所有