Skip to content

Instantly share code, notes, and snippets.

@aug2uag
Last active January 3, 2016 06:49
Show Gist options
  • Select an option

  • Save aug2uag/8425615 to your computer and use it in GitHub Desktop.

Select an option

Save aug2uag/8425615 to your computer and use it in GitHub Desktop.
Google v3 reverse geocoding api
// on button click, get and display user location to view
- (void)cityStateFinder
{
NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
// init location
locationManager.desiredAccuracy = kCLLocationAccuracyBest;
locationManager.distanceFilter = kCLDistanceFilterNone;
locationManager.delegate = self;
[locationManager startUpdatingLocation];
}
#pragma mark CLLocation delegate
- (void)locationManager:(CLLocationManager *)manager didUpdateToLocation:(CLLocation *)newLocation fromLocation:(CLLocation *)oldLocation
{
NSLog(@"%s [Line %d]", __PRETTY_FUNCTION__, __LINE__);
CLLocation* location = [manager location];
CLLocationCoordinate2D coordinate = [location coordinate];
NSString* urlString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", coordinate.latitude, coordinate.longitude]; // lat then long
NSURLRequest* request = [NSURLRequest requestWithURL:[NSURL URLWithString:urlString]];
[NSURLConnection sendAsynchronousRequest:request queue:[NSOperationQueue mainQueue] completionHandler:^(NSURLResponse *response, NSData *data, NSError *connectionError) {
if (data) {
NSDictionary* responseDictionary = [NSJSONSerialization JSONObjectWithData:data options:0 error:nil];
NSString* results = [NSString stringWithFormat:@" %@", [[responseDictionary valueForKey:@"results"][1] valueForKey:@"formatted_address"]];
// update gui location
dispatch_async(dispatch_get_main_queue(), ^{
[UIView animateWithDuration:0.3f animations:^{
locationField.text = results;
}];
});
}
}];
[locationManager stopUpdatingLocation];
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment