guruprasad gudluri
guruprasad gudluri

Reputation: 69

how to find closest locations between 5m to 10m range from array of locations in iOS?

this was my code and i want nearest locations from my current location in the range of 10meters and i want to add all the nearest locations with details of the person in response also add in array.thanks

and this is my response from server

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

like the above i am getting 5000 users from server ,after i filtering with 10 meters distance through my current location i need to display list in table view who are in below 10 meters range

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

Upvotes: 2

Views: 319

Answers (1)

Igor
Igor

Reputation: 1557

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

Upvotes: 2

Related Questions