Skip to content

Commit caccd2c

Browse files
committed
Make all SVPlacemark properties readonly, add name getter.
1 parent 2f1e9df commit caccd2c

File tree

3 files changed

+115
-72
lines changed

3 files changed

+115
-72
lines changed

SVGeocoder/SVGeocoder.m

Lines changed: 1 addition & 57 deletions
Original file line numberDiff line numberDiff line change
@@ -280,63 +280,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
280280

281281
if(results.count > 0) {
282282
[results enumerateObjectsUsingBlock:^(NSDictionary *result, NSUInteger idx, BOOL *stop) {
283-
SVPlacemark *placemark = [[SVPlacemark alloc] init];
284-
placemark.formattedAddress = [result objectForKey:@"formatted_address"];
285-
286-
NSArray *addressComponents = [result objectForKey:@"address_components"];
287-
288-
[addressComponents enumerateObjectsUsingBlock:^(NSDictionary *component, NSUInteger idx, BOOL *stop) {
289-
NSArray *types = [component objectForKey:@"types"];
290-
291-
if([types containsObject:@"street_number"])
292-
placemark.subThoroughfare = [component objectForKey:@"long_name"];
293-
294-
if([types containsObject:@"route"])
295-
placemark.thoroughfare = [component objectForKey:@"long_name"];
296-
297-
if([types containsObject:@"administrative_area_level_3"] || [types containsObject:@"sublocality"] || [types containsObject:@"neighborhood"])
298-
placemark.subLocality = [component objectForKey:@"long_name"];
299-
300-
if([types containsObject:@"locality"])
301-
placemark.locality = [component objectForKey:@"long_name"];
302-
303-
if([types containsObject:@"administrative_area_level_2"])
304-
placemark.subAdministrativeArea = [component objectForKey:@"long_name"];
305-
306-
if([types containsObject:@"administrative_area_level_1"]) {
307-
placemark.administrativeArea = [component objectForKey:@"long_name"];
308-
placemark.administrativeAreaCode = [component objectForKey:@"short_name"];
309-
}
310-
311-
if([types containsObject:@"country"]) {
312-
placemark.country = [component objectForKey:@"long_name"];
313-
placemark.ISOcountryCode = [component objectForKey:@"short_name"];
314-
}
315-
316-
if([types containsObject:@"postal_code"])
317-
placemark.postalCode = [component objectForKey:@"long_name"];
318-
319-
}];
320-
321-
NSDictionary *locationDict = [[result objectForKey:@"geometry"] objectForKey:@"location"];
322-
NSDictionary *boundsDict = [[result objectForKey:@"geometry"] objectForKey:@"bounds"];
323-
324-
CLLocationDegrees lat = [[locationDict objectForKey:@"lat"] doubleValue];
325-
CLLocationDegrees lng = [[locationDict objectForKey:@"lng"] doubleValue];
326-
placemark.coordinate = CLLocationCoordinate2DMake(lat, lng);
327-
placemark.location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
328-
329-
NSDictionary *northEastDict = [boundsDict objectForKey:@"northeast"];
330-
NSDictionary *southWestDict = [boundsDict objectForKey:@"southwest"];
331-
CLLocationDegrees northEastLatitude = [[northEastDict objectForKey:@"lat"] doubleValue];
332-
CLLocationDegrees southWestLatitude = [[southWestDict objectForKey:@"lat"] doubleValue];
333-
CLLocationDegrees latitudeDelta = fabs(northEastLatitude - southWestLatitude);
334-
CLLocationDegrees northEastLongitude = [[northEastDict objectForKey:@"lng"] doubleValue];
335-
CLLocationDegrees southWestLongitude = [[southWestDict objectForKey:@"lng"] doubleValue];
336-
CLLocationDegrees longitudeDelta = fabs(northEastLongitude - southWestLongitude);
337-
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
338-
placemark.region = MKCoordinateRegionMake(placemark.location.coordinate, span);
339-
283+
SVPlacemark *placemark = [[SVPlacemark alloc] initWithDictionary:result];
340284
[placemarks addObject:placemark];
341285
}];
342286
}

SVGeocoder/SVPlacemark.h

Lines changed: 17 additions & 14 deletions
Original file line numberDiff line numberDiff line change
@@ -13,20 +13,23 @@
1313

1414
@interface SVPlacemark : NSObject
1515

16-
@property (nonatomic, strong) NSString *formattedAddress;
17-
@property (nonatomic, strong) NSString *subThoroughfare;
18-
@property (nonatomic, strong) NSString *thoroughfare;
19-
@property (nonatomic, strong) NSString *subLocality;
20-
@property (nonatomic, strong) NSString *locality;
21-
@property (nonatomic, strong) NSString *subAdministrativeArea;
22-
@property (nonatomic, strong) NSString *administrativeArea;
23-
@property (nonatomic, strong) NSString *administrativeAreaCode;
24-
@property (nonatomic, strong) NSString *postalCode;
25-
@property (nonatomic, strong) NSString *country;
26-
@property (nonatomic, strong) NSString *ISOcountryCode;
16+
- (id)initWithDictionary:(NSDictionary*)dictionary;
2717

28-
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
29-
@property (nonatomic, readwrite) MKCoordinateRegion region;
30-
@property (nonatomic, strong) CLLocation *location;
18+
@property (nonatomic, strong, readonly) NSString *name;
19+
@property (nonatomic, strong, readonly) NSString *formattedAddress;
20+
@property (nonatomic, strong, readonly) NSString *subThoroughfare;
21+
@property (nonatomic, strong, readonly) NSString *thoroughfare;
22+
@property (nonatomic, strong, readonly) NSString *subLocality;
23+
@property (nonatomic, strong, readonly) NSString *locality;
24+
@property (nonatomic, strong, readonly) NSString *subAdministrativeArea;
25+
@property (nonatomic, strong, readonly) NSString *administrativeArea;
26+
@property (nonatomic, strong, readonly) NSString *administrativeAreaCode;
27+
@property (nonatomic, strong, readonly) NSString *postalCode;
28+
@property (nonatomic, strong, readonly) NSString *country;
29+
@property (nonatomic, strong, readonly) NSString *ISOcountryCode;
30+
31+
@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
32+
@property (nonatomic, readonly) MKCoordinateRegion region;
33+
@property (nonatomic, strong, readonly) CLLocation *location;
3134

3235
@end

SVGeocoder/SVPlacemark.m

Lines changed: 97 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -10,12 +10,108 @@
1010

1111
#import "SVPlacemark.h"
1212

13+
@interface SVPlacemark ()
14+
15+
@property (nonatomic, strong, readwrite) NSString *formattedAddress;
16+
@property (nonatomic, strong, readwrite) NSString *subThoroughfare;
17+
@property (nonatomic, strong, readwrite) NSString *thoroughfare;
18+
@property (nonatomic, strong, readwrite) NSString *subLocality;
19+
@property (nonatomic, strong, readwrite) NSString *locality;
20+
@property (nonatomic, strong, readwrite) NSString *subAdministrativeArea;
21+
@property (nonatomic, strong, readwrite) NSString *administrativeArea;
22+
@property (nonatomic, strong, readwrite) NSString *administrativeAreaCode;
23+
@property (nonatomic, strong, readwrite) NSString *postalCode;
24+
@property (nonatomic, strong, readwrite) NSString *country;
25+
@property (nonatomic, strong, readwrite) NSString *ISOcountryCode;
26+
27+
@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
28+
@property (nonatomic, readwrite) MKCoordinateRegion region;
29+
@property (nonatomic, strong, readwrite) CLLocation *location;
30+
31+
@end
32+
1333

1434
@implementation SVPlacemark
1535

1636
@synthesize formattedAddress, subThoroughfare, thoroughfare, subLocality, locality, subAdministrativeArea, administrativeArea, administrativeAreaCode, postalCode, country, ISOcountryCode, coordinate, location, region;
1737

18-
- (NSString*)description {
38+
- (id)initWithDictionary:(NSDictionary *)result {
39+
self.formattedAddress = [result objectForKey:@"formatted_address"];
40+
41+
NSArray *addressComponents = [result objectForKey:@"address_components"];
42+
43+
[addressComponents enumerateObjectsUsingBlock:^(NSDictionary *component, NSUInteger idx, BOOL *stopAddress) {
44+
NSArray *types = [component objectForKey:@"types"];
45+
46+
if([types containsObject:@"street_number"])
47+
self.subThoroughfare = [component objectForKey:@"long_name"];
48+
49+
if([types containsObject:@"route"])
50+
self.thoroughfare = [component objectForKey:@"long_name"];
51+
52+
if([types containsObject:@"administrative_area_level_3"] || [types containsObject:@"sublocality"] || [types containsObject:@"neighborhood"])
53+
self.subLocality = [component objectForKey:@"long_name"];
54+
55+
if([types containsObject:@"locality"])
56+
self.locality = [component objectForKey:@"long_name"];
57+
58+
if([types containsObject:@"administrative_area_level_2"])
59+
self.subAdministrativeArea = [component objectForKey:@"long_name"];
60+
61+
if([types containsObject:@"administrative_area_level_1"]) {
62+
self.administrativeArea = [component objectForKey:@"long_name"];
63+
self.administrativeAreaCode = [component objectForKey:@"short_name"];
64+
}
65+
66+
if([types containsObject:@"country"]) {
67+
self.country = [component objectForKey:@"long_name"];
68+
self.ISOcountryCode = [component objectForKey:@"short_name"];
69+
}
70+
71+
if([types containsObject:@"postal_code"])
72+
self.postalCode = [component objectForKey:@"long_name"];
73+
74+
}];
75+
76+
NSDictionary *locationDict = [[result objectForKey:@"geometry"] objectForKey:@"location"];
77+
NSDictionary *boundsDict = [[result objectForKey:@"geometry"] objectForKey:@"bounds"];
78+
79+
CLLocationDegrees lat = [[locationDict objectForKey:@"lat"] doubleValue];
80+
CLLocationDegrees lng = [[locationDict objectForKey:@"lng"] doubleValue];
81+
self.coordinate = CLLocationCoordinate2DMake(lat, lng);
82+
self.location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
83+
84+
NSDictionary *northEastDict = [boundsDict objectForKey:@"northeast"];
85+
NSDictionary *southWestDict = [boundsDict objectForKey:@"southwest"];
86+
CLLocationDegrees northEastLatitude = [[northEastDict objectForKey:@"lat"] doubleValue];
87+
CLLocationDegrees southWestLatitude = [[southWestDict objectForKey:@"lat"] doubleValue];
88+
CLLocationDegrees latitudeDelta = fabs(northEastLatitude - southWestLatitude);
89+
CLLocationDegrees northEastLongitude = [[northEastDict objectForKey:@"lng"] doubleValue];
90+
CLLocationDegrees southWestLongitude = [[southWestDict objectForKey:@"lng"] doubleValue];
91+
CLLocationDegrees longitudeDelta = fabs(northEastLongitude - southWestLongitude);
92+
MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
93+
self.region = MKCoordinateRegionMake(self.location.coordinate, span);
94+
95+
return self;
96+
}
97+
98+
- (NSString *)name {
99+
if(self.subThoroughfare && self.thoroughfare)
100+
return [NSString stringWithFormat:@"%@ %@", self.subThoroughfare, self.thoroughfare];
101+
else if(self.thoroughfare)
102+
return self.thoroughfare;
103+
else if(self.subLocality)
104+
return self.subLocality;
105+
else if(self.locality)
106+
return [NSString stringWithFormat:@"%@, %@", self.locality, self.administrativeAreaCode];
107+
else if(self.administrativeArea)
108+
return self.administrativeArea;
109+
else if(self.country)
110+
return self.country;
111+
return nil;
112+
}
113+
114+
- (NSString*)description {
19115
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
20116
formattedAddress, @"formattedAddress",
21117
subThoroughfare?subThoroughfare:[NSNull null], @"subThoroughfare",

0 commit comments

Comments
 (0)