使用CLGeocoder问题进行前向地理编码

时间:2012-01-13 05:29:24

标签: iphone ios ios5 mkmapview clgeocoder

我在使用geocodeAddressString函数在iOS5上使用正向地理编码时遇到问题。

我将MKMapView打开为模态视图,以显示从互联网抓取数据的不同位置。我的问题是当我解除模态视图,然后再次打开它。第二次尝试时,我的注释中只有大约一半实际放在地图上。第三次尝试,没有出现。

我觉得我的问题与内存管理和CLGeocoder的范围有关,但我无法理解。

我在包含MapView的视图控制器的ViewDidLoad函数中创建了所有注释。这是我用来获取地址坐标的代码:

int locationCount = 0;
for(NSDictionary *date in locations)
{
    CLGeocoder *geocoder = [[CLGeocoder alloc] init];
    [geocoder geocodeAddressString:[NSString stringWithFormat:@"%@, %@", [date objectForKey:@"venue"], [date objectForKey:@"location"]] completionHandler:^(NSArray *placemarks, NSError *error)
    {
        // if we find the exact location (including the venue string), create an annotation for the map
        if(placemarks && placemarks.count > 0)
        {
            CLPlacemark *topResult = [placemarks objectAtIndex:0];
            TourAnnotation *placemarkAnnotation = [[TourAnnotation alloc] initWithLocation:topResult.location andDetails:date];
            placemarkAnnotation.tag = locationCount;
            [tourMap addAnnotation:placemarkAnnotation];
        }
        // if not place an annotation at the center of the city
        else
        {
            [geocoder geocodeAddressString:[date objectForKey:@"location"] completionHandler:^(NSArray *placemarks, NSError *error)
             {
                 if(placemarks && placemarks.count > 0)
                 {
                     CLPlacemark *topResult = [placemarks objectAtIndex:0];
                     TourAnnotation *placemarkAnnotation = [[TourAnnotation alloc] initWithLocation:topResult.location andDetails:date];
                     placemarkAnnotation.tag = locationCount;
                     [tourMap addAnnotation:placemarkAnnotation];
                 }
             }];
        }
    }];
    ++locationCount;
}

任何建议都将不胜感激。

1 个答案:

答案 0 :(得分:0)

如果是内存问题,您考虑了注释是如何出列并重用的,那么地图视图就有一个委托方法来做这样的事情。

本教程可能会有所帮助。

Ray Wenderlich map tutorial