get the current city from coordinates iOS swift
Solution:
In iOS you can get the current city from your coordinates using the below function.
Code:
In iOS you can get the current city from your coordinates using the below function.
Code:
func getCurrentCity(loc:CLLocation , completion: @escaping (_ result: String) -> Void,
failure: (_ err:NSError) -> Void) {
// Get user's current location name
let geocoder = CLGeocoder()
var placeMark:String = ""
geocoder.reverseGeocodeLocation(loc) { (placemarksArray, error) in
if (placemarksArray?.count)! > 0 {
// City
let placemark = placemarksArray?.first
if let city = placemark?.addressDictionary!["City"] as? NSString {
print(city)
}
placeMark = placemark!.locality!
completion(placeMark)
}
}
}
You can get the location data you must have to check that if Location service is on and internet connection is on.
Comments
Post a Comment