发出前向地理编码多个地址

时间:2011-11-12 21:37:25

标签: objective-c ios5 geocode clgeocoder

我正在连接到一个基本上返回XML的远程Web服务。然后我将该XML解析为Property对象(想想真实的状态)

但是现在,Web服务仅为每个属性返回邮政编码。它没有提供我需要在地图中放置注释的坐标。我能够为邮政编码提供地址代码。但是,我的问题是它不允许我执行多个请求

这是我的代码

- (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];
}

出于某种原因,它只是对1个属性进行地理编码。没有相应的循环。

任何想法的人?

1 个答案:

答案 0 :(得分:6)

在前向地理功能上使用此功能。地理编码器需要被释放并再次初始化以开始一个新地址,希望这会有所帮助。

- (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];
             }];
  }
相关问题