Nearest point of intrest

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

  1. alexandergre macrumors member


    Jul 28, 2009
    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


    Oct 16, 2006
    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;

    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


    Jul 28, 2009
    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 *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 :
    - (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:)];

    thanx dude

Share This Page