I want to sort my tableview cells by nearest location to the user.
Let’s say i have 14 stores & i want to sort it by location.
Is there any example for doing something like this ?
Thanks!
Yes,
I have a tableview with 14 cells & i put names of 14 stores form plist in those cells with navigation control to other views with details (from the plist too).
The 14 stores are all over the country & i want to sort them by place according to my location in the country (with the assistant of the GPS of course)
I want that the neares store will be at the top of the table.
This is my sort code that i need to use but i can’t figure it out how & where to integrate it with the table.
For everyone who need that – feel free to use this code :).. alot of people need that:
- (NSComparisonResult)compareDistance:(id)obj
{
CLLocation* loc = [[CLLocation alloc]initWithLatitude:32.066157 longitude:34.777821];
StoreAnnotation* operand1 = self;
StoreAnnotation* operand2 = obj;
CLLocation* operand1Location = [[CLLocation alloc]initWithLatitude:operand1.coordinate.latitude longitude:operand1.coordinate.longitude];
CLLocationDistance distanceOfLocFromOperand1 = [loc distanceFromLocation:operand1Location];
NSLog(@"The distance of loc from op1 = %f meters", distanceOfLocFromOperand1);
CLLocation* operand2Location = [[CLLocation alloc]initWithLatitude:operand2.coordinate.latitude longitude:operand2.coordinate.longitude];
CLLocationDistance distanceOfLocFromOperand2 = [loc distanceFromLocation:operand2Location];
NSLog(@"The distance of loc from op2 = %f meters", distanceOfLocFromOperand2);
if (distanceOfLocFromOperand1 < distanceOfLocFromOperand2)
{
return NSOrderedAscending;
}
else if (distanceOfLocFromOperand1 > distanceOfLocFromOperand2)
return NSOrderedDescending;
else return NSOrderedSame;
}
It will be great if someone could help me with that..
Thanks alot.
OK so i got the code that doing the job with the cells together with the sorting code below.
I hope it will help to a lot of developers that need that.
Thanks for trying to help 🙂