gpt4 book ai didi

ios - 根据当前位置在 UITableview 中排序距离

转载 作者:行者123 更新时间:2023-11-29 02:43:38 25 4
gpt4 key购买 nike

我如何根据与当前位置的距离对数组进行排序并显示在表格 View 中。当我使用下面的代码进行排序时没有得到任何正确的结果,我得到的数组具有随机距离。有人可以指导我解决这个问题吗问题

-(void)sort_distance{
// Determine distance between current location and geofence location

[distance_array removeAllObjects];

NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
CLLocation *pinLocation;
CLLocation *userLocation;

for (int i=0; i<[self.geofences count]; i++) {

//dict =[self.geofences objectAtIndex:i];

CLLocationDegrees firstLat = [self.currentLatitude doubleValue];
CLLocationDegrees firstLong = [self.currentLongitude doubleValue];

CLLocationDegrees secondLat = [[[self.geofences objectAtIndex:i] objectForKey:@"lat"] doubleValue];
CLLocationDegrees secondLong = [[[self.geofences objectAtIndex:i] objectForKey:@"lon"] doubleValue];


pinLocation = [[CLLocation alloc]
initWithLatitude:secondLat
longitude:secondLong];

userLocation = [[CLLocation alloc]
initWithLatitude:firstLat
longitude:firstLong];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation]/1000;

//current distance is NSNumber


CLLocationDistance kilometers = distance /1000;
// or you can also use this..

distanceString = [[NSString alloc] initWithFormat: @"%.1f Km", kilometers];

NSLog(@"sa: %@", distanceString);


[distance_array addObject:distanceString];

[dict setObject:[NSNumber numberWithDouble:distance] forKey:@"newdistance"];


}
NSLog(@"distance array: %@", distance_array);







// Sorting the Geofence-Location depends on distance from current location


NSSortDescriptor * sort = [[NSSortDescriptor alloc] initWithKey:@"newdistance" ascending:YES];

NSArray *sortDescriptors = [NSArray arrayWithObject:sort];

sortedArray = [self.geofences sortedArrayUsingDescriptors:sortDescriptors];


}

我的输出是

距离数组:( "0.2 公里", "0.4 公里", "0.2 公里", "0.6 公里", "0.7 公里", "1.0 公里", “0.3 公里”)

提前致谢, 禅都。

最佳答案

-(void)sort_distance{
// Determine distance between current location and geofence location

[distance_array removeAllObjects];

NSMutableDictionary *dict=[[NSMutableDictionary alloc] init];
CLLocation *pinLocation;
CLLocation *userLocation;

for (int i=0; i<[self.geofences count]; i++) {

//dict =[self.geofences objectAtIndex:i];

CLLocationDegrees firstLat = [self.currentLatitude doubleValue];
CLLocationDegrees firstLong = [self.currentLongitude doubleValue];

CLLocationDegrees secondLat = [[[self.geofences objectAtIndex:i] objectForKey:@"lat"] doubleValue];
CLLocationDegrees secondLong = [[[self.geofences objectAtIndex:i] objectForKey:@"lon"] doubleValue];


pinLocation = [[CLLocation alloc]
initWithLatitude:secondLat
longitude:secondLong];

userLocation = [[CLLocation alloc]
initWithLatitude:firstLat
longitude:firstLong];
CLLocationDistance distance = [pinLocation distanceFromLocation:userLocation]/1000;

//current distance is NSNumber


CLLocationDistance kilometers = distance /1000;
// or you can also use this..

distanceString = [[NSString alloc] initWithFormat: @"%.1f km", kilometers];

NSLog(@"sa: %@", distanceString);


[distance_array addObject:distanceString];

[dict setObject:[NSNumber numberWithDouble:distance] forKey:@"newdistance"];


}
NSLog(@"distance array: %@", distance_array);







// Sorting the Geofence-Location depends on distance from current location


sortedArray = [distance_array sortedArrayUsingSelector:@selector(compare:)];
NSLog(@"%@",sortedArray);

}

关于ios - 根据当前位置在 UITableview 中排序距离,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/25418943/

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