iphone - 带有 CLGeocoder 问题的正向地理编码

标签 iphone ios ios5 mkmapview clgeocoder

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

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

我觉得我的问题与内存管理和 CLGeocoder 的范围有关,但我就是想不通。

我在包含 MapView 的 View Controller 的 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;
}

任何建议,将不胜感激。

最佳答案

如果是内存问题,您是否考虑过注释是如何出队和重用的, map View 有一个委托(delegate)方法来做这样的事情。

本教程可能会有所帮助。

Ray Wenderlich map tutorial

关于iphone - 带有 CLGeocoder 问题的正向地理编码,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/8846350/

相关文章:

mobile - iPhone 网站 - 但其他平台呢?

iphone - 如何创建 NSPredicate 来搜索日历中的事件?

iOS 使用别人的 .p12 和 .mobileprovision 发布 IPA

iphone - 如何检测 UITableView 上的 "quick touch"以便切换到全屏或从全屏切换?

ios - 如何在动画后将 View 返回到其原始状态?

objective-c - XMPP 不适用于 iOS 4.3 项目

objective-c - 将 NSDate 转换为其他格式

iphone - 使用 Organizer 上传到 iTunesConnect 时崩溃

ios - 如何从 URL 按顺序而不是一起将图像加载到 imageView?

ios - 如何让我在 iPhone Storyboard上开发的应用程序也能在 iPad 上运行?