gpt4 book ai didi

iOS CLLocation 代码运行顺序

转载 作者:行者123 更新时间:2023-11-29 03:20:30 24 4
gpt4 key购买 nike

我已经设法使用 GLGeocoder geocodeAddressString 获取了 2 个城市的坐标

从名称中检索到这 2 个城市的坐标后,我想计算它们之间的距离。但是我面临一个问题,因为 CLLocationDistancegeocodeAddressString 之前运行。

我如何确保仅在城市位置有之后才计算距离?

请看下面我的代码:

__block CLLocation *depLocation;
__block CLLocation *arrLocation;

CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder geocodeAddressString:@"Birmingham" completionHandler:^(NSArray* placemarks, NSError* error){
for (CLPlacemark* aPlacemark in placemarks)
{

depLocation = [[CLLocation alloc]
initWithLatitude:aPlacemark.location.coordinate.latitude
longitude:aPlacemark.location.coordinate.longitude];

}
}];

CLGeocoder *geocoder2 = [[CLGeocoder alloc] init];
[geocoder2 geocodeAddressString:@"Amritsar" completionHandler:^(NSArray* placemarks2, NSError* error){
for (CLPlacemark* aPlacemark2 in placemarks2)
{

arrLocation = [[CLLocation alloc]
initWithLatitude:aPlacemark2.location.coordinate.latitude
longitude:aPlacemark2.location.coordinate.longitude];

}
}];

CLLocationDistance distanceXYZ = [depLocation distanceFromLocation:arrLocation];
NSString *distanceLabel = [NSString stringWithFormat:@"Distance to point %4.0f m.", distanceXYZ];

最佳答案

简单的答案是嵌套所有的完成 block 。复制所有的geoCoder2 的东西并把它放在geocoder 完成 block ,然后复制distanceXYZdistanceLabel 到完成geocoder2

block

关于iOS CLLocation 代码运行顺序,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/21167657/

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