by Nicholas Duchon
The idea is to send some information about a location, say a zip code or partial address and get some information about the possible locations - particularly the latitude and longitude of the place(s).
- (IBAction)fromZip:(id)sender
{ // ND: connected to a button on the
display
self.labLat.text = @"234.432"; // ND: some junk
default values
self.latLong.text = @"23.332";
NSString * st = self.zipCode.text; // ND: get
string to search for from a text field in this example
NSLog(@"%@\n",
st);
// ND: print
it out just to be sure, debugging
// ND: create a geo object if it doesn't already exist
if (geo == NULL) geo = [[CLGeocoder
alloc] init];
// ND: inquiry based on string st - could be partial address,
zip code, etc.
[geo geocodeAddressString:st
// ND: results of query returned in array - generally just one
entry,
// so why bother with an array?
Who knows, just seems extra messy to me.
completionHandler:^(NSArray* placemarks, NSError* error)
// ND: the following is a block of code that handles the
return from the query event
// the
standard example uses the a for loop just in case there is
more than one return
// here -
handler is the name of a local instance method and placemarks
is the name of the
// of the
array of locations returned from the query.
{[self handler:placemarks];}
];
} // end
method fromZip - the one that is connected to a button in my
example - not shown
// ND: the
method called in the block above - at least the block is
shorter this way.
- (void) handler:(NSArray *) placemarks{
// ND: go through all the locations returned, as noted
above, this is generally just one place
for (CLPlacemark* aPlacemark in
placemarks)
{
// ND: the following two line put a description of the
location on the debug display
// this should be removed in your
production code
NSString * st2
= [aPlacemark description];
NSLog(@"%@\n", st2);
// ND: this is one way to get the location coordinates in
double variables
// notice that the
class relationships are rather complex - try the quick help to
see!
double lat =
aPlacemark.location.coordinate.latitude;
double lon =
aPlacemark.location.coordinate.longitude;
// ND: the following is one way to put some information about
the location
// returned on the display, labLat,
latLong, town and state are names of outlets
// on the display I have created as a
demo
self.labLat.text = [[NSString alloc] initWithFormat:@"%f", lat];
self.latLong.text =
[[NSString alloc] initWithFormat:@"%f", lon];
self.town.text =
aPlacemark.locality;
self.state.text =
aPlacemark.administrativeArea;
// Process the placemark.
}
} // end
handler