Reverse Geo-coding – obtaining Location details from the Address in Google Map

Following snip of code is surely gonna help you to grab the location from an address supplied to it. Yes, Just copy & paste the code supplied below this image & It will work like a charm.
1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 |
#pragma mark - GetLocation + (CLLocationCoordinate2D) geoCodeUsingAddress: (NSString *) address { CLLocationCoordinate2D myLocation; // -- modified from the stackoverflow page - we use the SBJson parser instead of the string scanner -- NSString *esc_addr = [address stringByAddingPercentEscapesUsingEncoding: NSUTF8StringEncoding]; NSString *req = [NSString stringWithFormat: @"http://maps.googleapis.com/maps/api/geocode/json?sensor=false&address=%@", esc_addr]; NSDictionary *googleResponse = [[NSString stringWithContentsOfURL: [NSURL URLWithString: req] encoding: NSUTF8StringEncoding error: NULL] JSONValue]; NSDictionary *resultsDict = [googleResponse valueForKey: @"results"]; // get the results dictionary NSDictionary *geometryDict = [ resultsDict valueForKey: @"geometry"]; // geometry dictionary within the results dictionary NSDictionary *locationDict = [ geometryDict valueForKey: @"location"]; // location dictionary within the geometry dictionary // -- you should be able to strip the latitude & longitude from google's location information (while understanding what the json parser returns) -- NSArray *latArray = [locationDict valueForKey: @"lat"]; NSString *latString = [latArray lastObject]; // (one element) array entries provided by the json parser NSArray *lngArray = [locationDict valueForKey: @"lng"]; NSString *lngString = [lngArray lastObject]; // (one element) array entries provided by the json parser myLocation.latitude = [latString doubleValue]; // the json parser uses NSArrays which don't support "doubleValue" myLocation.longitude = [lngString doubleValue]; return myLocation; } |