gpt4 book ai didi

ios - 如何从 iOS 中的位置数组中找到 5m 到 10m 范围内的最近位置?

转载 作者:塔克拉玛干 更新时间:2023-11-02 19:57:38 24 4
gpt4 key购买 nike

这是我的代码,我想要距离我当前位置 10 米范围内最近的位置,我想添加所有最近的位置以及响应者的详细信息也添加到数组中。谢谢

这是服务器的回复

{
born = 0;
completed = 0;
"emergency_id" = "-1";
eyes = "";
feet = 0;
"first_name" = Neil;
glasses = No;
hair = "";
inch = 0;
language = "<null>";
"last_name" = Ferris;
latitude = "30.1456987";
longitude = "76.69916";
marks = "";
"member_id" = 21634;
picture = "";
"profile_id" = 5826;
type = 1;
updated = "2015-09-16 06:32:08";
weight = 0;
}

像上面一样,我从服务器获得了 5000 个用户,在我通过当前位置过滤 10 米距离后,我需要在表格 View 中显示 10 米以下范围内的列表

- (void)getdata
{
NSString * apiURLStr =[NSString stringWithFormat:@"<MY API URL>"];

NSString *outputStr = [NSString stringWithContentsOfURL:[NSURL URLWithString:apiURLStr] encoding:NSUTF8StringEncoding error:nil];
NSDictionary *response;
if (outputStr == nil)
{
return;

}

NSData* data = [outputStr dataUsingEncoding:NSUTF8StringEncoding];
NSError *error;
response = [NSJSONSerialization JSONObjectWithData:data options:kNilOptions error:&error];

NSMutableArray *array=[[NSMutableArray alloc]init];
CLLocation *closestLocation = nil;
for (NSDictionary *dict in response)
{
NSString *latitude=[dict valueForKey:@"latitude"];
NSString *longitude=[dict valueForKey:@"longitude"];

CLLocation *location = [[CLLocation alloc] initWithLatitude:latitude.doubleValue
longitude:longitude.doubleValue];

[array addObject:location];
}

CLLocationDistance closestLocationDistance = DBL_MIN;

for (CLLocation *location in array)
{
if (!closestLocation) {
closestLocation = location;
closestLocationDistance = [currentUserLocation distanceFromLocation:location];
continue;
}
CLLocationDistance currentDistance = [currentUserLocation distanceFromLocation:location];
if (currentDistance < closestLocationDistance)
{
closestLocation = location;
closestLocationDistance = currentDistance;
}
}

NSLog(@"closestLocation %@",closestLocation.description);
NSLog(@"closestLocationDistance %f", closestLocationDistance);
}

最佳答案

thanks for answer bro,but i want list of closest locations in below 10 meters range.

NSMutableArray *locationsInRange = [NSMutableArray new];

for (CLLocation *location in array)
{
CLLocationDistance currentDistance = [currentUserLocation distanceFromLocation:location];
if (currentDistance < 10)
{
[locationsInRange addObject:location];
}
}

NSLog(@"locations in 10 metters %@",locationsInRange.description);

关于ios - 如何从 iOS 中的位置数组中找到 5m 到 10m 范围内的最近位置?,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/32758916/

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