gpt4 book ai didi

objective-c - 查询 Google 纬度/经度需要很长时间,使得 UIViewController 需要永远加载

转载 作者:行者123 更新时间:2023-11-29 04:28:48 24 4
gpt4 key购买 nike

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

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

这是我的代码:

- (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)当用户按下 Go 按钮时,您分配初始化您的下一个 View Controller

2)假设您有一个下载纬度/经度信息的方法和一个渲染您的 pin 的方法,在下一个 View Controller 的 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)];
}

关于objective-c - 查询 Google 纬度/经度需要很长时间,使得 UIViewController 需要永远加载,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/11979374/

24 4 0
Copyright 2021 - 2024 cfsdn All Rights Reserved 蜀ICP备2022000587号
广告合作:1813099741@qq.com 6ren.com