1

I am developing an Iphone application in which i am using GoogleAPI to find the nearest Restrooms within 500 meters. But the thing i want to do is to find the places in sorted pattern starting from places whose distance is less from current location.

Thanks.

rinku yadav
  • 121
  • 3
  • 10
  • 1
    http://stackoverflow.com/questions/2864682/how-use-google-api-in-iphone-to-query-hotels-around-my-place – Mehul Mistri Feb 01 '12 at 10:08
  • Thanks for your reply but i need to know that how can i identify that places i retrived is in sorted manner according to nearest distance from my location. – rinku yadav Feb 01 '12 at 10:28
  • your could use R-Tree to find places within a range and/or k-d tree to find m-nearest places e.g., [SQLite R*Tree index](http://www.sqlite.org/rtree.html), [Millions of 3D points: How to find the 10 of them closest to a given point?](http://stackoverflow.com/q/2486093/4279). These data-structures are usually hidden behind API calls on the server. – jfs Feb 01 '12 at 11:22

2 Answers2

1

Assume restroomLocations is the coordinatesArray, userLocation is the user current location, and that you know how to use the CoreLocation framework to get all users' current location. The following method will help you:

-(NSMutableArray *)getRelevantRestrooms:(NSArray *)restroomLocations withUsersLocation:(CLLocation *)myLocation{
    NSMutableArray *resultsArray=[[NSMutableArray alloc]init];

    for (CLLocation *location in restroomLocations) {
        if ([location distanceFromLocation:myLocation]<=500){
            [resultsArray addObject:location];
        }
    }

    return [resultsArray autorelease];
}
Gilles 'SO- stop being evil'
  • 104,111
  • 38
  • 209
  • 254
0

Use "&rankby=distance" to get result in sorted form.

    var urlString = "https://maps.googleapis.com/maps/api/place/nearbysearch/json?"
    urlString = urlString + "location=" + "\(lat)" + "," + "\(lng)"
    urlString = urlString + "&rankby=distance"
    urlString = urlString + "&type=" + poiType
    urlString = urlString + "&key=" + GOOGLE_API_KEY
Yogesh Rathore
  • 147
  • 1
  • 7