OStack程序员社区-中国程序员成长平台

标题: ios - 如何从iOS中的位置数组中找到5m到10m范围内的最近位置? [打印本页]

作者: 菜鸟教程小白    时间: 2022-12-12 16:19
标题: ios - 如何从iOS中的位置数组中找到5m到10m范围内的最近位置?

这是我的代码,我希望距离当前位置最近的位置在 10 米范围内,并且我想添加所有最近的位置以及此人的详细信息作为响应还添加 array.thanks

这是我来自服务器的响应

{
    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);
}



Best Answer-推荐答案


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/






欢迎光临 OStack程序员社区-中国程序员成长平台 (https://ostack.cn/) Powered by Discuz! X3.4