THEKRYS
THEKRYS

Reputation: 1

Geocoder Finding Location Sometimes

I'm trying to go to another area after a location is input (zip, address, city, st). It works-SOMETIMES. After I move the map (zoom out or move around), I'm not able to search for an area using the location. I thought I should call startUpdatingLocation again, but that didn't seem to work. Any help would be greatly appreciated considering this is my first time working with maps. Code below:

let geocoder = CLGeocoder()
    geocoder.geocodeAddressString(self.zipCode.text!.capitalized) {
        placemarks, error in
        if error == nil {
        let placemark = placemarks?.first
        let lat = placemark?.location?.coordinate.latitude
        let lon = placemark?.location?.coordinate.longitude
    
        let center = CLLocationCoordinate2D(latitude: lat ?? 0.0, longitude: lon ?? 0.0)
        
        print("\(self.zipCode.text) lat \(lat) long \(lon)")
    let mRegion = MKCoordinateRegion(center: center, span: MKCoordinateSpan(latitudeDelta: 0.2, longitudeDelta: 0.2))
        self.mapView.setRegion(mRegion, animated: true)
        }
        else{
            print("Error finding location \(self.zipCode.text!)")

Upvotes: 0

Views: 233

Answers (2)

Akilan C B
Akilan C B

Reputation: 371

I would say that is due to the number of requests made within a small time interval. I experimented with Apple's location framework for a long time and found that a single app can't send multiple geocoding request within short time. If you try so, you will get an error.

Upvotes: 0

Giovanni Luigi
Giovanni Luigi

Reputation: 164

I would need a little more context to give you an exact answer (like where in the code are you calling this, how often are you doing, what is the string dress value), but as far as I can see the problem could be:

  • lat or Lon could be nil and would default to 0.0 (?? operator)
  • all the code is running inside the closure, so it could be a scope problem

Also, taking a quick look at Apple documentation I found the following:

After initiating a forward-geocoding request, do not attempt to initiate another forward- or reverse-geocoding request. Geocoding requests are rate-limited for each app, so making too many requests in a short period of time may cause some of the requests to fail. When the maximum rate is exceeded, the geocoder passes an error object with the value CLError.Code.network to your completion handler.

So it could be an problem with the amount of the requests you are doing in a sort amount of time

Upvotes: 2

Related Questions