获得经度和纬度

时间:2017-07-18 08:33:39

标签: ios swift geolocation mapkit core-location

我在美国使用它时遇到了应用程序崩溃的问题。在所有其他国家,相同的代码正在运作。崩溃发生在线:

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

1 个答案:

答案 0 :(得分:1)

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

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