简体   繁体   English

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

[英]Forward Geocoding with CLGeocoder Issues

I'm having an issue using forward geocoding on iOS5 with the geocodeAddressString function. 我在使用geocodeAddressString函数在iOS5上使用正向地理编码时遇到问题。

I have a MKMapView open up as a modal view to display different locations with data grabbed from the internet. 我打开了一个MKMapView作为模式视图,以显示不同的位置以及从互联网上获取的数据。 My issue is when I dismiss the modal view, and then open it up again. 我的问题是当我关闭模式视图,然后再次打开它时。 On second try, only about half of my annotations are actually placed on the map. 第二次尝试时,实际上只有大约一半的注释被放置在地图上。 On a third try, none show up. 第三次尝试,没有任何显示。

I have a feeling my issue here has to do with memory management and the scope of the CLGeocoder, but I just can't figure it out. 我觉得我的问题与内存管理和CLGeocoder的范围有关,但我无法弄清楚。

I create all of my annotations in the ViewDidLoad function of the view controller containing the MapView. 我在包含MapView的视图控制器的ViewDidLoad函数中创建所有注释。 Here is the code I use to get the coordinates of the addresses: 这是我用来获取地址坐标的代码:

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

Any suggestions would be appreciated. 任何建议,将不胜感激。

If it's memory issues have you considered how the annotation are dequeued and reused, the map view has a delegate method to do such a thing. 如果是内存问题,您是否已考虑过如何使注释出列和重新使用,则地图视图中有一个委托方法来执行此操作。

This tutorial may be of some assistance. 本教程可能会有所帮助。

Ray Wenderlich map tutorial 雷·温德利希(Ray Wenderlich)地图教程

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM