0

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)
Asperi
  • 228,894
  • 20
  • 464
  • 690
  • `geoCoder.reverseGeocodeLocation` is asynchronous look up "Completion Handlers" `return` is usually used for synchronous work. – lorem ipsum Jul 26 '22 at 14:06
  • @loremipsum u didn't answer my question – user14300555 Jul 26 '22 at 14:26
  • 2
    I didn't write an answer just a comment. I just gave you a search term so you can try and solve your own issue. You haven't provided a [Minimal Reproducible Example](https://stackoverflow.com/help/minimal-reproducible-example) I am not going to create a Minimal Reproducible Answer. – lorem ipsum Jul 26 '22 at 14:31

0 Answers0