Last active
January 3, 2016 06:49
-
-
Save aug2uag/8425615 to your computer and use it in GitHub Desktop.
Google v3 reverse geocoding api
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| // 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