我在我的城市有一些商店的经度和纬度。在确定设备/用户位置和商店之间的距离后,所有数据都显示在表格中。默认情况下,当应用程序启动时,我需要调用按距离对商店进行排序的方法,然后重新加载 tableview。请建议我如何以及在何处(方法序列)调用排序方法,因为有时尚未收到设备/用户位置数据并且表格排序错误。英语不好,抱歉
要确定我正在使用CLLocationManagerDelegate 的位置:
- (void)locationManager:(CLLocationManager *)manager
didUpdateToLocation:(CLLocation *)newLocation
fromLocation:(CLLocation *)oldLocation {
myNewLocation = newLocation;
[_tableView reloadData];
}
然后 (CCLocationDistance)distanceFromLocation: tableview:cellforIndexPath 中的方法
viewDidLoad中调用的排序方法:
- (NSArray*)getSortedArray:(NSArray*)shops byLocation:(CLLocation*)userLocation{
NSArray *sortedArray;
sortedArray = [shops sortedArrayUsingComparator:^NSComparisonResult(id obj1, id obj2){
Shop *dict1 = (Shop*)obj1;
Shop *dict2 = (Shop*)obj2;
CLLocation *shopLocation = [[CLLocation alloc]initWithLatitude:[[dict1 lat]doubleValue]
longitude:[[dict1 lon]doubleValue]];
CLLocation *shopLocation2 = [[CLLocation alloc]initWithLatitude:[[dict2 lat]doubleValue]
longitude:[[dict2 lon]doubleValue]];
CLLocationDistance key1 = [userLocation distanceFromLocation:shopLocation];
CLLocationDistance key2 = [userLocation distanceFromLocation:shopLocation2];
NSNumber *num1 = [NSNumber numberWithDouble:key1];
NSNumber *num2 = [NSNumber numberWithDouble:key2];
return [num1 compare:num2];
}];
return sortedArray;
}