Note that there are some explanatory texts on larger screens.

plurals
  1. POOrder results of CLGeoCoder geocodeAddressString by nearest location
    text
    copied!<p>I'm sure that I can figure this out based on code that I have implemented in PHP in the past, but I was hoping that someone has either done this and can provide an example or knows of a way to use the iOS SDK to accomplish it.</p> <p>Here's my relevant code:</p> <pre><code>CLGeocoder *geocoder = [[CLGeocoder alloc] init]; CLLocationDistance dist = _searchDistance; CLLocationCoordinate2D point = self.locationManager.location.coordinate; CLRegion *region = [[CLRegion alloc] initCircularRegionWithCenter:point radius:dist identifier:@"Hint Region"]; [geocoder geocodeAddressString:address inRegion:region completionHandler:^(NSArray *placemarks, NSError *error) { if( error ) { // Push errors to the user } else { NSLog( [NSString stringWithFormat:@"Got results: %@", placemarks] ); } }]; </code></pre> <p>This pushes to the log something like the following:</p> <pre><code>( "300 Oak St, Phoenix, OR 97535-5722, United States @ &lt;+42.26953820,-122.81554259&gt; +/- 100.00m, region (identifier &lt;+42.26953820,-122.81554106&gt; radius 27.00) &lt;+42.26953820,-122.81554106&gt; radius 27.00m", "300 Oak St, Ashland, OR 97520, United States @ &lt;+42.19955633,-122.71289484&gt; +/- 100.00m, region (identifier &lt;+42.19955633,-122.71289484&gt; radius 138.42) &lt;+42.19955633,-122.71289484&gt; radius 138.42m", "300 Oak St, Jacksonville, OR 97530, United States @ &lt;+42.31236366,-122.97179130&gt; +/- 100.00m, region (identifier &lt;+42.31236366,-122.97179130&gt; radius 138.33) &lt;+42.31236366,-122.97179130&gt; radius 138.33m", "300 Oak St, Central Point, OR 97502, United States @ &lt;+42.37422514,-122.91427182&gt; +/- 100.00m, region (identifier &lt;+42.37422514,-122.91427182&gt; radius 138.29) &lt;+42.37422514,-122.91427182&gt; radius 138.29m", "300 Oak St, Rogue River, OR 97537, United States @ &lt;+42.43621216,-123.16864522&gt; +/- 100.00m, region (identifier &lt;+42.43621216,-123.16864522&gt; radius 138.24) &lt;+42.43621216,-123.16864522&gt; radius 138.24m" ) </code></pre> <p>In this case, the second result is the closest to my current location. I would like to have this list ordered by matches that are closest to my current location. I'm reasonably confident that I know how to do the math to figure out which location is closest, but I was hoping for a shortcut. Iterating through this array and sorting the results is going to be somewhat heavy, so I'd like to avoid that if possible.</p> <p><strong>EDIT (complete answer):</strong></p> <p>@Akash gave me the bit of information I was seeking, so I marked that answer as the correct one. If anyone is interested in sorting the results (rather than just getting the closest location), I put the unsorted results into an <code>NSDictionary</code> using the distance as a key, then used <code>compare</code> to sort the keys and then put the original <code>placemark</code> array elements into an output array ordered by the key to which they were associated in the <code>NSDictionary</code>. Here's the complete code (and the complete answer to my question):</p> <pre><code>-(NSMutableArray *)distanceSortPlacemarks:(NSArray *)inputArray fromLocation:(CLLocation *)originLocation { NSMutableArray *returnValue = [[NSMutableArray alloc] initWithCapacity:[inputArray count]]; NSMutableDictionary *sortDictionary = [[NSMutableDictionary alloc] initWithCapacity:[inputArray count]]; for( int i = 0; i &lt; [inputArray count]; i++ ) { CLPlacemark *currentPlacemark = [inputArray objectAtIndex:i]; [sortDictionary setObject:currentPlacemark forKey:[NSNumber numberWithDouble:[currentPlacemark.location distanceFromLocation:originLocation]] ]; } NSArray *sortedKeys = [[sortDictionary allKeys] sortedArrayUsingSelector:@selector(compare:)]; for( int i = 0; i &lt; [sortedKeys count]; i++ ) { CLPlacemark *currentPlacemark = [sortDictionary objectForKey:[sortedKeys objectAtIndex:i]]; [returnValue insertObject:currentPlacemark atIndex:i]; } return returnValue; } </code></pre> <p>I know this could be a little tighter (some of the variables aren't absolutely necessary), but I tend to err on the side of making code easier to understand at quick glance.</p>
 

Querying!

 
Guidance

SQuiL has stopped working due to an internal error.

If you are curious you may find further information in the browser console, which is accessible through the devtools (F12).

Reload