获取经度和纬度

赢范

我在美国使用该应用程序时遇到过崩溃。在所有其他国家/地区,相同的代码正在运行。崩溃是在线发生的:

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

我真的看不出有什么问题,特别是 cos 与美国有关而不是与其他县有关。任何帮助将非常感谢。

我的 UserLocation.swift 看起来像这样:

import UIKit
import MapKit

public class GPSLocation {

static let sharedInstance = GPSLocation()

//MARK: Public variables

public var intermediateLatitude: String?
public var intermediateLongitude: String?
public var intermediateCountry: String?
public var intermediateCity: String?
public var intermediateTimeZone: String?

//MARK: Get Longitude, Country Code and City name

func getGPSLocation(completition: @escaping () -> Void) {

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

        let locManager = manager
        var currentLocation: CLLocation!
        locManager.desiredAccuracy = kCLLocationAccuracyBest

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

            currentLocation = locManager.location

            if currentLocation != nil {
                // Get longitude & latitude
                let latitude = String(format: "%.7f", currentLocation.coordinate.latitude)
                let longitude = String(format: "%.7f", currentLocation.coordinate.longitude)
                self.intermediateLatitude = latitude
                self.intermediateLongitude = longitude
                // debugPrint("Latitude:",latitude)
                // debugPrint("Longitude:",longitude)

                // Get local time zone GMT
                let localTimeZoneAbbreviation = TimeZone.current.abbreviation() ?? ""  // "GMT-2"
                let indexStartOfText = localTimeZoneAbbreviation.index(localTimeZoneAbbreviation.startIndex, offsetBy: 3) // 3
                let timeZone = localTimeZoneAbbreviation.substring(from: indexStartOfText) // "-2"
                self.intermediateTimeZone = timeZone
                // debugPrint("GMT:",timeZone)

                // Get Country code and City
                let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
                fetchCountryAndCity(location: location) { countryCode, city in
                    self.intermediateCountry = countryCode
                    self.intermediateCity = city
                    // debugPrint("Country code:",countryCode)
                    // debugPrint("City:",city)

                    completition()
                }
            } else {
                // Location is NIL
            }
        }
    }
    locManager.delegate = self // and conform protocol
    locationManager.startUpdatingLocation()
}

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

您需要在委托方法中检查位置

func locationManager(manager: CLLocationManager, didUpdateLocations locations: [CLLocation]) {
          manager.stopUpdatingLocation() // if you dont want continuously update

            currentLocation = manager.location
         let location = CLLocation(latitude: currentLocation.coordinate.latitude, longitude: currentLocation.coordinate.longitude)
    fetchCountryAndCity(location: location) { countryCode, city in
        self.intermediateCountry = countryCode
        self.intermediateCity = city
        // debugPrint("Country code:",countryCode)
        // debugPrint("City:",city)

        completition()
    }
   }

并设置委托

locManager.delegate = self // and conform protocol 
locationManager.startUpdatingLocation()

本文收集自互联网,转载请注明来源。

如有侵权,请联系 [email protected] 删除。

编辑于
0

我来说两句

0 条评论
登录 后参与评论

相关文章