获取经度和纬度
我在美国使用它时遇到了应用程序崩溃。在所有其他国家,相同的代码正在工作。崩溃是发生在行:获取经度和纬度
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)
}
}
}
}
您需要在委托方法
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()
我是初学者,很抱歉,如果我用你的代码改变整个班级(代码)...它会不断发送位置?我需要将这个类的结果从long,lat,countryCode和city只传递给其他类。只有当我从其他课程调用它时才更新。 –
如果你不想连续,那么你可以停止更新cllocationmanage这个方法** didUpdateLocations **。 – KKRocks
所以我应该把函数从你的答案放在didFinishLaunchingWithOptions中的AppDelegate.swift中。或者在ViewController中,我需要func getGPSLocation的结果..? –
是否已确认' locManager.location'不是'nil'? –
如果我包裹整个块如果locManager.location不是从零//获取经度和纬度直到Completition()结束如果声明...? –
首先找出问题所在。如果locManager.location为零,则问题与String(格式:...)无关。然后搜索“CLLocationManager位置为零”,这里已经有很多问答。 –