gpt4 book ai didi

objective-c - CLGeocoder 多个地址?

转载 作者:行者123 更新时间:2023-11-29 13:37:34 25 4
gpt4 key购买 nike

我需要对两个不同的地址进行地理编码,但我需要在同一范围/方法内获得结果。如何将相同的变量 location1location2 指向同一 block 内。这是我当前的代码...

 - (IBAction)calculateDistance {
[textFieldDestination resignFirstResponder];
NSString *origin = [textFieldOrigin text];
if (origin) {
CLGeocoder *geoCode = [[CLGeocoder alloc] init];

[geoCode geocodeAddressString:origin completionHandler: ^(NSArray *placemarks, NSError *error) {
if (!error) {
CLPlacemark *place = [placemarks objectAtIndex:0];
CLLocation *location = place.location;
CLLocation *location1 = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
} else {
NSString *string = [NSString stringWithFormat:@"%@ - also make sure you have inputted a valid city", [error localizedDescription]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}];
}

NSString *destination = [textFieldDestination text];
if (destination) {
CLGeocoder *geoCode2 = [[CLGeocoder alloc] init];

[geoCode2 geocodeAddressString:destination completionHandler:^(NSArray *placemarks, NSError *error) {
if (!error) {
CLPlacemark *place = [placemarks objectAtIndex:0];
CLLocation *location = place.location;
CLLocation *location2 = [[CLLocation alloc] initWithLatitude:location.coordinate.latitude longitude:location.coordinate.longitude];
} else {
NSString *string = [NSString stringWithFormat:@"%@ - also make sure you have inputted a valid city", [error localizedDescription]];
UIAlertView *alert = [[UIAlertView alloc] initWithTitle:@"Error" message:string delegate:self cancelButtonTitle:@"Ok" otherButtonTitles:nil, nil];
[alert show];
}
}];
}

CLLocationDistance distanceM = [location1 distanceFromLocation:location2]; // obviously error so you can see that I need location1 and location2 in the same block.... but how!?
[metreDistance setText:[NSString stringWithFormat:@"%f", distanceM]];
}

最佳答案

我找到了如何实现这一点。对于每个地理编码 block ,我都指向一个具有特定参数的方法。很简单,只需要使用逻辑!

关于objective-c - CLGeocoder 多个地址?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/10138325/

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