查询谷歌的lat / lon需要很长时间,让UIViewController永远加载

时间:2012-08-16 01:08:14

标签: objective-c ios mkmapview xcode4.3

我正在向Google查询地图视图的纬度/经度值。这可能需要很长时间,具体取决于用户放入应用程序的地址数量,因为地图会查看所有这些地址。我还介绍了在for循环中查询之间的短暂延迟,以确保Google为我提供了良好的结果(如果查询得太快,它不会给你有效的纬度/长点,而是0,0)。

因为这需要很长时间,一旦用户按下按钮转到此UIViewController,它需要很长时间,我担心用户会认为应用程序已损坏。理想情况下,我希望ViewController能够“显示”但是在地图上有一个地图(最初没有注释),地图上有一个动画活动指示器,直到它完成查询,然后加载注释。这样,用户可以在等待加载地图时查看视图上的其他信息。

这是我的代码:

- (void) viewDidLoad {
    [mapScroll startAnimating]; // activity indicator outlet
    [self makeMap];
    // ... etc.
}

- (void)makeMap {
allRegions = [[NSMutableArray alloc] init];
[self getDistinctRegions]; //generates allRegions array

double maxLat = -90;
double maxLon = -180;
double minLat = 90;
double minLon = 180;

for (int i=0; i<[allRegions count]; i++) {
    NSString *tempString = [allRegions objectAtIndex:i];
    NSString *searchString = [tempString stringByReplacingOccurrencesOfString:@" " withString:@"%20"];
    NSString *queryURL = [[NSString alloc] initWithFormat:@"http://maps.google.com/maps/geo?output=csv&q=%@",searchString];
    NSString *queryResults = [[NSString alloc] initWithContentsOfURL: [NSURL URLWithString:queryURL] encoding: NSUTF8StringEncoding error:nil];
    NSArray *queryData = [queryResults componentsSeparatedByString:@","];

    if ([queryData count] == 4) {
        double latitude = [[queryData objectAtIndex:2] doubleValue];
        double longitude = [[queryData objectAtIndex:3] doubleValue];

        if (latitude > maxLat) maxLat = latitude;
        if (latitude < minLat) minLat = latitude;
        if (longitude > maxLon) maxLon = longitude;
        if (longitude < minLon) minLon = longitude;

        CLLocationCoordinate2D coordinate;
        coordinate.latitude = latitude;
        coordinate.longitude = longitude;

        MKPlacemark *marker;
        marker = [[MKPlacemark alloc] initWithCoordinate:coordinate addressDictionary:nil];
        [mapView addAnnotation:marker];

        NSLog(@"%d",i);

        [NSThread sleepForTimeInterval:0.12];
    }
}

MKCoordinateRegion overallRegion;

double latSpan = abs(maxLat - minLat);
double lonSpan = abs(maxLon - minLon);
double avgLatitude = (maxLat+minLat)/2;
double avgLongitude = (maxLon+minLon)/2;

overallRegion.center.latitude = avgLatitude;
overallRegion.center.longitude = avgLongitude;
overallRegion.span.latitudeDelta = latSpan;
overallRegion.span.longitudeDelta = lonSpan;

[mapView setRegion:overallRegion animated:YES];

[mapScroll stopAnimating];
mapScroll.hidden = TRUE;
}

1 个答案:

答案 0 :(得分:1)

嗯,快速而肮脏的解决方案,但这是一般过程:

1)当用户按下Go按钮时,你可以分配初始化你的下一个视图控制器

2)假设你有一个方法可以下载你的lat / long信息和一个渲染你的pin的方法,你可以在下一个视图控制器的viewDidLoad方法中执行以下操作:

-(void)viewDidLoad
{
    ....
    [self performSelectorInBackground:@selector(downloadMapData) withObject:nil];
}

// your download map data method
-(void)downloadMapData
{
    // download data from Google API. I assume you know how to do this part.

    // when done, add map annotation points to map on the main thread
    // interface changes must be done on the main thread
    [self performSelectorOnMainThread:@selector(addPinToMap)];
}