Saturday, 20 February 2016

Geocoding Google map in iOS

#define GEOCODING_URI @"https://maps.googleapis.com/maps/api/geocode/json?key=&language=en-US&sensor=true&address=" // TODO: add DEV_KEY for Geocoding
NSString *actionURI = [NSString stringWithFormat:@"%@%@",GEOCODING_URI, self.geocodeTextField.text];
NSLog(@"ACTION URL: %@",actionURI);
NSString *encodedURI = [actionURI stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
AFHTTPRequestOperationManager*manager = [AFHTTPRequestOperationManager manager];
[manager GET:encodedURI parameters:nil success:^(AFHTTPRequestOperation *operation, id responseObject) {
NSLog(@"JSON: %@", responseObject); 
NSDictionary *jsonDict = (NSDictionary *)responseObject;
if (!jsonDict)
   return;
if ([jsonDict isKindOfClass:[NSDictionary class]] == NO)
   NSAssert(NO, @"Expected a dictionary, got %@", NSStringFromClass([jsonDict class]));     
if ([jsonDict[@"status"] isEqualToString:@"OK"]) {
    NSArray *locGeometryResult=[[jsonDict valueForKeyPath:@"results.geometry"] objectAtIndex:0];
    NSDictionary *locationDict = [locGeometryResult valueForKey:@"location"];
    NSLog(@"JSON: %@", locationDict.description);
    CLLocationCoordinate2D coordinate = CLLocationCoordinate2DMake([locationDict[@"lat"] doubleValue], [locationDict[@"lng"] doubleValue]);
    GMSCameraUpdate *updatedCamera = [GMSCameraUpdate setTarget:coordinate zoom:17];
    [self.mapView animateWithCameraUpdate:updatedCamera];
} else {
        NSLog(@"No location found.");
}
} failure:^(AFHTTPRequestOperation *operation, NSError *error) {
        NSLog(@"Error: %@"); 
}];

No comments:

Post a Comment