3

I am connecting to a remote web service which basically returns an XML back. I am then parsing that XML into a Property object (think real state sort of thing)

But now, the web service returns a postal code for each property alone. It does not provide a coordinate which is what I need to place an annotation in the map. I am able to geocode an address provided a postal code. However, my problem is it is not allowing me to do multiple requests

Here's my code

- (void)processProperties:(Property *)property {

    [geocoder geocodeAddressString:property.postalCode
                 completionHandler:^(NSArray* placemarks, NSError* error){
                     placemark = [placemarks lastObject];
                     for (CLPlacemark* aPlacemark in placemarks)
                     {
                         [sublet setLatitude:aPlacemark.location.coordinate.latitude];
                         [sublet setLongitude:aPlacemark.location.coordinate.longitude];  
                     }
                 }];
}


- (void)addAnnotations:(NSArray *)objects {
    CLLocationDegrees lat;
    CLLocationDegrees longitude;
    CLLocationCoordinate2D mCoords;
    NSString *fullAddress;

    // Add the annotations found nearby
    for (Property *property in objects) {

        [self processProperties:property];
        lat = property.latitude;
        longitude = property.longitude;

        fullAddress = [NSString stringWithFormat:@"%@ %@ %@", property.houseNumber, @" ", property.streetName];
        [self createAnnotationWithCoords:mCoords :fullAddress :[NSString stringWithFormat:@"$%.2f", property.rent]];
    }
    zoomLevel = 0.1;
    mCoords = CLLocationCoordinate2DMake(lat,longitude);
    MKCoordinateRegion region = MKCoordinateRegionMake(mCoords,MKCoordinateSpanMake(zoomLevel,zoomLevel));

   [self.mapView setRegion:region animated:YES];
}

For some reason it's just geocoding 1 property. Is not going through the loop accordingly.

Any ideas folks?

MrShoot
  • 843
  • 1
  • 9
  • 21

1 Answers1

6

Use this on your Forward Geo Function. geocoder needs to be release and initialized again to start a new address, hope this helps.

- (void)processProperties:(Property *)property { 
CLGeocoder *geocoder = [[CLGeocoder alloc] init]; 
[geocoder geocodeAddressString:property.postalCode
             completionHandler:^(NSArray* placemarks, NSError* error){
                 placemark = [placemarks lastObject];
                 for (CLPlacemark* aPlacemark in placemarks)
                 {
                     [sublet setLatitude:aPlacemark.location.coordinate.latitude];
                     [sublet setLongitude:aPlacemark.location.coordinate.longitude];  
                 }
                 [geocoder release];
             }];
  }
Silent
  • 641
  • 1
  • 16
  • 34
  • Apple seems to recommend that only one geocoding be done per app session, which is very limiting. You're right that they seem to enforce this by only allowing an instance one call. Creating multiple instances solves the problem, but it sure is an odd thing to have to do. – akaru Dec 19 '12 at 18:40