/
SVGeocoder.m
155 lines (99 loc) · 5.11 KB
/
SVGeocoder.m
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
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
//
// SVGeocoder.m
//
// Created by Sam Vermette on 07.02.11.
// Copyright 2011 Sam Vermette. All rights reserved.
//
#import "SVGeocoder.h"
#import "JSONKit.h"
@interface SVGeocoder ()
@property (nonatomic, retain) NSString *requestString;
@property (nonatomic, assign) NSMutableData *responseData;
@property (nonatomic, assign) NSURLConnection *rConnection;
@property (nonatomic, retain) NSURLRequest *request;
@end
@implementation SVGeocoder
@synthesize delegate, requestString, responseData, rConnection, request;
#pragma mark -
- (SVGeocoder*)initWithCoordinate:(CLLocationCoordinate2D)coordinate {
self.requestString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?latlng=%f,%f&sensor=true", coordinate.latitude, coordinate.longitude];
NSLog(@"SVGeocoder -> %@", self.requestString);
return self;
}
- (SVGeocoder*)initWithAddress:(NSString *)address inRegion:(MKCoordinateRegion)region {
self.requestString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&bounds=%f,%f|%f,%f&sensor=true",
address,
region.center.latitude-(region.span.latitudeDelta/2.0),
region.center.longitude-(region.span.longitudeDelta/2.0),
region.center.latitude+(region.span.latitudeDelta/2.0),
region.center.longitude+(region.span.longitudeDelta/2.0)];
NSLog(@"SVGeocoder -> %@", self.requestString);
return self;
}
- (SVGeocoder*)initWithAddress:(NSString*)address {
self.requestString = [NSString stringWithFormat:@"http://maps.googleapis.com/maps/api/geocode/json?address=%@&sensor=true", address];
NSLog(@"SVGeocoder -> %@", self.requestString);
return self;
}
#pragma mark -
- (void)setDelegate:(id <SVGeocoderDelegate>)newDelegate {
delegate = newDelegate;
}
- (void)startAsynchronous {
NSString *escapedString = [self.requestString stringByAddingPercentEscapesUsingEncoding:NSUTF8StringEncoding];
self.request = [NSURLRequest requestWithURL:[NSURL URLWithString:escapedString]];
responseData = [[NSMutableData alloc] init];
rConnection = [[NSURLConnection alloc] initWithRequest:request delegate:self startImmediately:YES];
}
#pragma mark -
#pragma mark NSURLConnectionDelegate
- (void)connection:(NSURLConnection *)connection didReceiveData:(NSData *)data {
[responseData appendData:data];
}
- (void)connectionDidFinishLoading:(NSURLConnection *)connection {
NSError *jsonError = NULL;
NSDictionary *responseDict = [responseData objectFromJSONData];
if(responseDict == nil || [responseDict valueForKey:@"results"] == nil || [[responseDict valueForKey:@"results"] count] == 0) {
[self connection:connection didFailWithError:jsonError];
return;
}
NSDictionary *addressDict = [[[responseDict valueForKey:@"results"] objectAtIndex:0] valueForKey:@"address_components"];
NSDictionary *coordinateDict = [[[[responseDict valueForKey:@"results"] objectAtIndex:0] valueForKey:@"geometry"] valueForKey:@"location"];
float lat = [[coordinateDict valueForKey:@"lat"] floatValue];
float lng = [[coordinateDict valueForKey:@"lng"] floatValue];
NSMutableDictionary *formattedAddressDict = [[NSMutableDictionary alloc] init];
for(NSDictionary *component in addressDict) {
NSArray *types = [component valueForKey:@"types"];
if([types containsObject:@"street_number"])
[formattedAddressDict setValue:[component valueForKey:@"long_name"] forKey:(NSString*)kABPersonAddressStreetKey];
if([types containsObject:@"route"])
[formattedAddressDict setValue:[[formattedAddressDict valueForKey:(NSString*)kABPersonAddressStreetKey] stringByAppendingFormat:@" %@",[component valueForKey:@"long_name"]] forKey:(NSString*)kABPersonAddressStreetKey];
if([types containsObject:@"locality"])
[formattedAddressDict setValue:[component valueForKey:@"long_name"] forKey:(NSString*)kABPersonAddressCityKey];
if([types containsObject:@"administrative_area_level_1"])
[formattedAddressDict setValue:[component valueForKey:@"long_name"] forKey:(NSString*)kABPersonAddressStateKey];
if([types containsObject:@"postal_code"])
[formattedAddressDict setValue:[component valueForKey:@"long_name"] forKey:(NSString*)kABPersonAddressZIPKey];
if([types containsObject:@"country"]) {
[formattedAddressDict setValue:[component valueForKey:@"long_name"] forKey:(NSString*)kABPersonAddressCountryKey];
[formattedAddressDict setValue:[component valueForKey:@"short_name"] forKey:(NSString*)kABPersonAddressCountryCodeKey];
}
}
SVPlacemark *placemark = [[SVPlacemark alloc] initWithCoordinate:CLLocationCoordinate2DMake(lat, lng) addressDictionary:formattedAddressDict];
[formattedAddressDict release];
NSLog(@"SVGeocoder -> Found Placemark");
[self.delegate geocoder:self didFindPlacemark:placemark];
[placemark release];
}
- (void)connection:(NSURLConnection *)connection didFailWithError:(NSError *)error {
NSLog(@"SVGeocoder -> Failed with error: %@, (%@)", [error localizedDescription], [[request URL] absoluteString]);
[self.delegate geocoder:self didFailWithError:error];
}
#pragma mark -
- (void)dealloc {
self.request = nil;
[responseData release];
[rConnection release];
[super dealloc];
}
@end