Nearest point of intrest

Discussion in 'iOS Programming' started by alexandergre, Dec 20, 2009.

  1. alexandergre macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #1
    There are 5 restaurants with diffrent longitudes and latitudes.
    You current location is x.
    And the application wants to find the 3 nearest restaurand to X.


    the Question is:
    Where and how shall I save the 5 restaurants with their longs and lats? in a database? array?
    so that the 'distance calculation process' willl be as easy as possible.
     
  2. KoolStar macrumors demi-god

    KoolStar

    Joined:
    Oct 16, 2006
    Location:
    Kentucky
    #2
    Store the data in coreData or dictionaries. This is the equation that I used to calculate distance between gps points for some of my stuff.
    dist = (((acos(sin(lat1 * pi() /180) * sin(lat2 * pi() /180) + cos(lat1 * pi() /180) * cos(lat2 * pi() /180) * cos((lon1- lon2) * pi() /180)) * 180.0) / pi()) * 60 * 1.1515) * 1.609344;

    lat1/lon1
    lat2/lon2
    are the respected pair of gps coordinates.
    The last 1.609344 converts the distance to miles otherwise its in KM.

     
  3. alexandergre thread starter macrumors member

    alexandergre

    Joined:
    Jul 28, 2009
    #3
    Thank you KoolStar. I have looked at coreData and dictionaries. But still I decided to go with SQL. Every thing works well except SORTING
    I have a class and it stores the info from the database:

    Object.m
    Code:
    Object *myObject = [[Object alloc] initWithPrimaryKey:primaryKey];
    myObject.Name = [NSString stringWithUTF8String:(char *)sqlite3_column_text(selectstmt, 1)];	
    myObject.Price=sqlite3_column_int(selectstmt, 2);
    [appDelegate.ObjectArray addObject:myObject];
    [coffeeObj release];

    Basicly all the items in the database are stored in a NSMutableArray called ObjectArray.and to show the item into a UITableView :
    Code:
    - (UITableViewCell *)tableView:(UITableView *)tableView cellForRowAtIndexPath:(NSIndexPath *)indexPath {
    
    %%%
    
    //Get the object from the array.
    Object *myObject = [appDelegate.ObjectArray objectAtIndex:indexPath.row];
    [cell setText:[NSString stringWithFormat:@"%i€    -     %@",myObject.price, myObject.Name]];
    



    the items are shown correctly in the UITableView.
    but how can I sort them inside the array?(I think I have to sort the array where I typed %%%)

    this doesnt work:
    NSArray *sortedArray =[appDelegate.ObjectArray sortedArrayUsingSelector:mad:selector(caseInsensitiveCompare:)];

    tips?
    thanx dude
     

Share This Page