首页
学习
活动
专区
圈层
工具
发布
首页
学习
活动
专区
圈层
工具
MCP广场
社区首页 >问答首页 >从谷歌地理编码中加速addAnnotations?

从谷歌地理编码中加速addAnnotations?
EN

Stack Overflow用户
提问于 2011-05-02 03:54:18
回答 3查看 305关注 0票数 0

我在我的应用程序中有以下代码,它通过Google地理编码向地图视图添加了一些注释:

代码语言:javascript
运行
复制
for (PlaceObject *info in mapLocations) {

    NSString * addressOne = info.addressOne;
    NSString * name = info.name;

    NSString * address = [addressOne stringByAppendingString:@",London"];

    NSError * error;

    NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

    NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
    NSArray *listItems = [locationString componentsSeparatedByString:@","];

    double latitude = 0.0;
    double longitude = 0.0;

    if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
        latitude = [[listItems objectAtIndex:2] doubleValue];
        longitude = [[listItems objectAtIndex:3] doubleValue];

    } else {
        //Error handling

    }        

    CLLocationCoordinate2D coordinate;
    coordinate.latitude = latitude;
    coordinate.longitude = longitude;
    MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease];
    [mapViewLink addAnnotation:annotation];

}

这是可行的,但是视图需要很长时间才会出现,因为它似乎要等待数组中的所有对象都循环通过(大约有80个)。

有没有办法加载视图,然后在创建管脚时添加管脚?

EN

回答 3

Stack Overflow用户

回答已采纳

发布于 2011-05-02 04:05:03

这样如何:

代码语言:javascript
运行
复制
for (PlaceObject *info in mapLocations) {

    dispatch_queue_t queue = dispatch_get_global_queue(DISPATCH_QUEUE_PRIORITY_DEFAULT, 0);
    dispatch_async(queue, ^{

        // GET ANNOTATION INFOS
        NSString * addressOne = info.addressOne;
        NSString * name = info.name;

        NSString * address = [addressOne stringByAppendingString:@",London"];

        NSError * error;

        NSString *urlString = [NSString stringWithFormat:@"http://maps.google.com/maps/geo?q=%@&output=csv", [address stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding]];

        NSString *locationString = [NSString stringWithContentsOfURL:[NSURL URLWithString:urlString ] encoding:NSASCIIStringEncoding error:&error];
        NSArray *listItems = [locationString componentsSeparatedByString:@","];

        double latitude = 0.0;
        double longitude = 0.0;

        if([listItems count] >= 4 && [[listItems objectAtIndex:0] isEqualToString:@"200"]) {
            latitude = [[listItems objectAtIndex:2] doubleValue];
            longitude = [[listItems objectAtIndex:3] doubleValue];

        } else {
            //Error handling

        }        

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude;
        coordinate.longitude = longitude;
        MyLocation *annotation = [[[MyLocation alloc] initWithName:name address:address coordinate:coordinate] autorelease]; 

        dispatch_sync(dispatch_get_main_queue(), ^{

            // ADD ANNOTATION
            [mapViewLink addAnnotation:annotation];

        });

    });
}

但请考虑仔细检查此代码。关于保持线程的安全和避免EXC_BAD_ACCESS,你应该做一些明确的事情。

票数 1
EN

Stack Overflow用户

发布于 2011-05-02 04:02:38

这段代码在你的应用程序中的什么地方执行?

我建议使用NSURLConnection,并在接收和解析数据之后添加注释。如果你不熟悉NSURLConnection,可以在这里查看更多信息:http://developer.apple.com/library/mac/#documentation/Cocoa/Conceptual/URLLoadingSystem/Tasks/UsingNSURLConnection.html

票数 0
EN

Stack Overflow用户

发布于 2011-05-02 04:03:03

是的,你犯了一个典型的错误,那就是在主(UI)线程中做长时间的工作(从URL净加载)。将这项工作移到后台线程中,然后让主线程添加引脚。这可以通过几种方式,通过创建另一个显式线程,使用块的GCD,或者只是在后台线程中的performSelector。最简单的方法可能是在后台performSelector,然后当它有东西要添加到地图时,在主线程上执行performSelector。

票数 0
EN
页面原文内容由Stack Overflow提供。腾讯云小微IT领域专用引擎提供翻译支持
原文链接:

https://stackoverflow.com/questions/5850930

复制
相关文章

相似问题

领券
问题归档专栏文章快讯文章归档关键词归档开发者手册归档开发者手册 Section 归档