views:

287

answers:

2

Hi all,

I am trying to devellopp an app showing the nearest poi to the users location.

My app is now capable of: showing a map (Mapkit) with 110 annotations and the user's location.

Furthermore, I have a TableView at the bottom of my screen, to choose between the different annotations. However, they're listed by number, and not by proximity to the user's location, which is my wish.

My research have led me to think that I should use CLLocation for getting the user's location. I then have really no idea on how to do it next, using getDistancefrom.

My annotations are stored on SecondViewController.m, using this form:

-(void)loadOurAnnotations
{
    CLLocationCoordinate2D workingCoordinate;

    workingCoordinate.latitude = XX.XXXXX;
    workingCoordinate.longitude = XX.XXXXX;
    SecondViewAnnotation *POI1 = [[SecondViewAnnotation alloc] initWithCoordinate:workingCoordinate];
    [POI1 setTitle:@"POI"];
    [POI1 setSubtitle:@"StreetName"];
    [POI1 setAnnotationType:SecondViewAnnotationTypePOI1];

    [mapView addAnnotation:POI1]; 
    ...

and so on to POI110.

Could someone please give me a clue? I'm a total newbie!

Thank you in advance for your help,

+1  A: 

NSArray class provide several methods for sorting its contents. For example you can sort your annotations using sortedArrayUsingFunction:context:. To do that you need to implement a function that compares 2 arbitrary elements (here we will also use current user location passed to the function via context parameter):

NSInteger distanceSort(id annot1, id annot2, void *context){
    SecondViewAnnotation* poi1 = (SecondViewAnnotation*)annot1;
    SecondViewAnnotation* poi2 = (SecondViewAnnotation*)annot2;
    CLLocation* userLocation = (CLLocation*)context;
    CLLocation* location1 = [[CLLocation alloc] initWithLatitude:poi1.coordinate.latitude,
                      longitude:poi1.coordinate.longitude];
    CLLocation* location2 = [[CLLocation alloc] initWithLatitude:poi2.coordinate.latitude,
                      longitude:poi2.coordinate.longitude];

    CLLocationDistance distance1 = [userLocation getDistanceFrom:location1];
    CLLocationDistance distance2 = [userLocation getDistanceFrom:location2];
    [location1 release];
    [location2 release];

    if (distance1 > distance2)
       return NSOrderedDescending;

    if (distance1 < distance2)
       return NSOrderedAscending;

    return NSOrderedSame;       
}

Then somewhere in your code you can do the following:

if (mapView.userLocation.location)
    //Do not forget to release `sortedArray` when you do not need it to avoid memory leak.
    sortedArray = [[mapView.annotations sortedArrayUsingFunction:distanceSort context:mapView.userLocation.location] retain];
Vladimir
As I wrote, I am total newbie. Could you please tell me if the code will fit for the 110 poi, or does it work only for poi1 and poi2? THANK YOU VERY MUCH FOR YOUR HELP!
danskcollignon
Yes, it should work (although I haven't tested it). Do you need more comments on code to understand how it works?
Vladimir
I'm not sure where I should place the codes. Should I just include them like that in SecondViewController.m ? thank you for your time!
danskcollignon
I suppose you can add that code right after you've added all annotations to the mapview. And later you should use sortedArray as a data source for your tableview.
Vladimir
And think about a way to create annotations in a loop - if you create each of 110 annotations separately it will be a real pain to read/fix/change/add/test/etc your programm
Vladimir
A: 

hi Vladmir can u put more comments i want know how does it work plizzz i'm total newbie too. thanks

pams37