When I use this method, for update annotation title,i always have error when I move map to another city or country:
Error is:
The operation couldn’t be completed. (kCLErrorDomain error 2.)
Yes, I have seen similar posts on this topic. However, there are no solutions to the problem, and yes, I checked the Internet connection, and tried to test on the device.
Method is:
and second problem, my "addressString" always empty, also when I have placemark, and "city"/"state" values printed, but addressString always empty. But latitude and longitude always print, when I move map.
func convertLatLongToAddress(latitude: Double,longitude: Double) -> String {
var addressString: String = ""
let geoCoder = CLGeocoder()
let location = CLLocation(latitude: latitude, longitude: longitude)
geoCoder.reverseGeocodeLocation(location, completionHandler: { (placemarks, error) -> Void in
if let error = error {
debugPrint("ERROR IS: \(error.localizedDescription)")
}
// Place details
var placeMark: CLPlacemark?
placeMark = placemarks?[0]
// City
if let city = placeMark?.locality {
print(city)
addressString = city
}
// State
if let state = placeMark?.administrativeArea {
print(state)
addressString += ", \(state)"
}
})
debugPrint("LATITUDE: \(latitude)")
debugPrint("LONGITUDE: \(longitude)")
return addressString
}
Where I use this method:
Map(coordinateRegion: $manager.region,
annotationItems: manager.annotationItems
) { place in
MapAnnotation(coordinate: manager.region.center) {
PinView(text: "\(manager.convertLatLongToAddress(latitude: manager.region.center.latitude, longitude: manager.region.center.longitude))")
.offset(x: 0, y: -15)
}
}
.frame(width: proxy.size.width, height: proxy.size.height, alignment: .top)
.cornerRadius(8)