【问题标题】:geocode method not executing second time in the for loop地理编码方法未在 for 循环中第二次执行
【发布时间】:2012-09-27 07:16:22
【问题描述】:

我编写了以下代码来找出国家列表的坐标。

  int count=[objCountries.countryName count];
CLGeocoder *geoCode = [[CLGeocoder alloc] init];
for(int i=0;i<count;i++)
{
    NSString *strCountry=[[NSString alloc]initWithString:[objCountries.countryName objectAtIndex:i]];




        [geoCode geocodeAddressString:strCountry completionHandler:^(NSArray *placemarks, NSError *error)
    {
        if (!error)
        {
            CLPlacemark *place = [placemarks objectAtIndex:0];
            CLLocation *location = place.location;
            CLLocationCoordinate2D coord = location.coordinate;

            NSString *tempLati=[[NSString alloc]initWithFormat:@"%g",coord.latitude];
            NSString *tempLongi=[[NSString alloc]initWithFormat:@"%g",coord.longitude];
            NSLog(@"-------------------------");
            NSLog(@"Country : %@",strCountry);
            NSLog(@" Latitude : %@ ",tempLati);
            NSLog(@" Longitude : %@ ",tempLongi);

            [objCountries.countryLatitude addObject:tempLati];
            [objCountries.countryLongitude addObject:tempLongi];
            [db insertAsiaCountry:strCountry :tempLati :tempLongi];
        }
    }];

}

}

在我的 countryName 数组中,有 20 个对象可用

问题: 它只在第一次工作得很好。但是第二次执行for循环时 [geocode geo....] 方法未调用。我不明白该怎么办?请帮忙。谢谢你,先生。

【问题讨论】:

    标签: iphone ios5 clgeocoder


    【解决方案1】:

    在这行之后..

        [db insertAsiaCountry:strCountry :tempLati :tempLongi];
    

    添加

    [geoCode cancelGeocode];
    

    即使发生错误,尝试在“for”循环中分配 geoCoder

    【讨论】:

    • 谢谢亲爱的。你是完美的。
    【解决方案2】:

    您不能在for loop 中调用任何其他Method,因为循环不会等到工作完成。在您的代码中,您在 for loop 中调用 geoCode 方法是错误的。尝试在您的第一个方法完成的地方调用该方法。

    【讨论】:

    • wait_fences:未能收到回复:10004003 即将到来。这是我的第一个地图应用程序,所以我无法理解。该怎么办 ?请帮忙。我可以通过其他方式完成这项任务吗?怎么样?
    • 答案修改为相关问题的链接
    • 首先删除你的循环尝试一次带断点......并检查当你的方法工作完成时哪个是最后调用的方法......
    【解决方案3】:

    CLGeocoder 不打算在您尝试时以批处理方式使用,因此后续请求可能会被 Apple 服务器拒绝。查看 CLGeocoder 参考文档,了解如何使用它。

    将此数据need-a-list-of-all-countries-in-the-world 复制并粘贴到电子表格中,并将其另存为 csv。将此文件添加到项目中。然后您的应用程序读取文件并将注释创建为数组,然后您将其添加到地图视图中。

    【讨论】:

    • 我想在世界上所有国家显示别针。我可以使用另一种方法来执行上述任务吗?请给我一些提示。
    • 有一个可用的数据文件可供您使用 - 请参阅我的答案中的链接
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2019-11-24
    • 1970-01-01
    • 2017-03-13
    • 1970-01-01
    • 2012-11-06
    • 1970-01-01
    • 1970-01-01
    相关资源
    最近更新 更多