加速来自 Google 地理编码的 addAnnotations? [英] Speed up addAnnotations from Google Geocoding?

查看:21
本文介绍了加速来自 Google 地理编码的 addAnnotations?的处理方法,对大家解决问题具有一定的参考价值,需要的朋友们下面随着小编来一起学习吧!

问题描述

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

I have the following code in my app which adds a number of Annotations to a mapview via Google geocoding:

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 个).

This works, however it takes a long time for the view to appear as it seems to wait until all objects in the array have been looped through (there are about 80).

有没有办法让视图加载,然后在创建引脚时添加它们?

Is there any way to make the view load and then for the pins to be added as they are created ?

推荐答案

这个怎么样:

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,您绝对应该做一些事情.

But please consider to double-check this code. There are definitily things you should do about keeping the threads safe and avoid EXC_BAD_ACCESS.

这篇关于加速来自 Google 地理编码的 addAnnotations?的文章就介绍到这了,希望我们推荐的答案对大家有所帮助,也希望大家多多支持IT屋!

查看全文
登录 关闭
扫码关注1秒登录
发送“验证码”获取 | 15天全站免登陆