Created
February 27, 2011 23:33
-
-
Save zssz/846696 to your computer and use it in GitHub Desktop.
Demo
This file contains 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
// | |
// IZGoogleGeocoder.m | |
// JSON Google Geocoder | |
// | |
// Created by Zsombor Szabó on 2/11/11. | |
// Copyright 2011 IZE. All rights reserved. | |
// | |
#import "IZGoogleGeocoder.h" | |
#import "IZHTTPOperation.h" | |
#import "NSURL+Parameters.h" | |
#import "JSON.h" | |
@implementation IZGoogleGeocoder | |
- (id)init | |
{ | |
self = [super init]; | |
if (self) | |
{ | |
operationQueue_ = [[NSOperationQueue alloc] init]; | |
} | |
return self; | |
} | |
- (void)dealloc | |
{ | |
[operationQueue_ cancelAllOperations]; | |
[operationQueue_ release]; | |
[super dealloc]; | |
} | |
- (void)searchForAddress:(NSString *)address // required | |
location:(CLLocation *)location // required | |
boundsSouthWestLocation:(CLLocation *)boundsSouthWestLocation // optional | |
boundsNorthEastLocation:(CLLocation *)boundsNorthEastLocation // optional | |
region:(NSString *)region // optional | |
language:(NSString *)language // optional | |
sensor:(BOOL)sensor // required | |
completion:(void (^)(id result, NSError *error))block | |
{ | |
// Sanity check | |
if (address == nil && location == nil) | |
return; | |
// Either address or location, and not both at the same time | |
if (address && location) | |
return; | |
// Construct parameters for the URL | |
NSMutableDictionary *parameters = [[NSMutableDictionary alloc] init]; | |
if (address) | |
{ | |
[parameters setObject:address forKey:@"address"]; | |
} | |
if (location) | |
{ | |
[parameters setObject:[NSString stringWithFormat:@"%.6f,%.6f", location.coordinate.latitude, location.coordinate.longitude] | |
forKey:@"latlng"]; | |
} | |
if (boundsSouthWestLocation && boundsNorthEastLocation) | |
{ | |
[parameters setObject:[NSString stringWithFormat:@"%.6f,%.6f|%.6f,%.6f", | |
boundsSouthWestLocation.coordinate.latitude, boundsSouthWestLocation.coordinate.longitude, | |
boundsNorthEastLocation.coordinate.latitude, boundsNorthEastLocation.coordinate.longitude] | |
forKey:@"bounds"]; | |
} | |
if (region) | |
{ | |
[parameters setObject:region forKey:@"region"]; | |
} | |
if (language) | |
{ | |
[parameters setObject:language forKey:@"language"]; | |
} | |
if (sensor) | |
{ | |
[parameters setObject:@"true" forKey:@"sensor"]; | |
} | |
else | |
{ | |
[parameters setObject:@"false" forKey:@"sensor"]; | |
} | |
NSURL *requestURL = [[NSURL alloc] initWithBaseString:kGoogleAPIMapsGeocodeJSONBaseURL parameters:parameters]; | |
[parameters release]; | |
__block IZHTTPOperation *httpOperation = [[IZHTTPOperation alloc] initWithURL:requestURL]; | |
[requestURL release]; | |
// Store the current queue, so we know on which queue to call when completed | |
__block NSOperationQueue *currentQueue = [NSOperationQueue currentQueue]; | |
[httpOperation setCompletionBlock:^ | |
{ | |
if ([httpOperation isCancelled]) | |
return; | |
NSError *error = httpOperation.error; | |
NSData *data = httpOperation.responseBody; | |
id json = nil; | |
if (data) | |
{ | |
NSString *jsonString = [[NSString alloc] initWithData:data encoding:NSUTF8StringEncoding]; | |
json = [jsonString JSONValue]; | |
[jsonString release]; | |
} | |
// We finished, now let's call the block parameter on the calling queue (usually the main thread) | |
[currentQueue addOperationWithBlock:^ | |
{ | |
block(json, error); | |
}]; | |
}]; | |
[operationQueue_ addOperation:httpOperation]; | |
[httpOperation release]; | |
} | |
- (BOOL)isLoading | |
{ | |
return ([operationQueue_ operationCount] > 0); | |
} | |
- (void)cancel | |
{ | |
[operationQueue_ cancelAllOperations]; | |
} | |
@end |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment