Skip to content
Browse files

Make all SVPlacemark properties readonly, add name getter.

  • Loading branch information...
1 parent 2f1e9df commit caccd2c6dee40bdb829a37a2eb8a9635ae7de88c @samvermette samvermette committed
Showing with 115 additions and 72 deletions.
  1. +1 −57 SVGeocoder/SVGeocoder.m
  2. +17 −14 SVGeocoder/SVPlacemark.h
  3. +97 −1 SVGeocoder/SVPlacemark.m
View
58 SVGeocoder/SVGeocoder.m
@@ -280,63 +280,7 @@ - (void)connectionDidFinishLoading:(NSURLConnection *)connection {
if(results.count > 0) {
[results enumerateObjectsUsingBlock:^(NSDictionary *result, NSUInteger idx, BOOL *stop) {
- SVPlacemark *placemark = [[SVPlacemark alloc] init];
- placemark.formattedAddress = [result objectForKey:@"formatted_address"];
-
- NSArray *addressComponents = [result objectForKey:@"address_components"];
-
- [addressComponents enumerateObjectsUsingBlock:^(NSDictionary *component, NSUInteger idx, BOOL *stop) {
- NSArray *types = [component objectForKey:@"types"];
-
- if([types containsObject:@"street_number"])
- placemark.subThoroughfare = [component objectForKey:@"long_name"];
-
- if([types containsObject:@"route"])
- placemark.thoroughfare = [component objectForKey:@"long_name"];
-
- if([types containsObject:@"administrative_area_level_3"] || [types containsObject:@"sublocality"] || [types containsObject:@"neighborhood"])
- placemark.subLocality = [component objectForKey:@"long_name"];
-
- if([types containsObject:@"locality"])
- placemark.locality = [component objectForKey:@"long_name"];
-
- if([types containsObject:@"administrative_area_level_2"])
- placemark.subAdministrativeArea = [component objectForKey:@"long_name"];
-
- if([types containsObject:@"administrative_area_level_1"]) {
- placemark.administrativeArea = [component objectForKey:@"long_name"];
- placemark.administrativeAreaCode = [component objectForKey:@"short_name"];
- }
-
- if([types containsObject:@"country"]) {
- placemark.country = [component objectForKey:@"long_name"];
- placemark.ISOcountryCode = [component objectForKey:@"short_name"];
- }
-
- if([types containsObject:@"postal_code"])
- placemark.postalCode = [component objectForKey:@"long_name"];
-
- }];
-
- NSDictionary *locationDict = [[result objectForKey:@"geometry"] objectForKey:@"location"];
- NSDictionary *boundsDict = [[result objectForKey:@"geometry"] objectForKey:@"bounds"];
-
- CLLocationDegrees lat = [[locationDict objectForKey:@"lat"] doubleValue];
- CLLocationDegrees lng = [[locationDict objectForKey:@"lng"] doubleValue];
- placemark.coordinate = CLLocationCoordinate2DMake(lat, lng);
- placemark.location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
-
- NSDictionary *northEastDict = [boundsDict objectForKey:@"northeast"];
- NSDictionary *southWestDict = [boundsDict objectForKey:@"southwest"];
- CLLocationDegrees northEastLatitude = [[northEastDict objectForKey:@"lat"] doubleValue];
- CLLocationDegrees southWestLatitude = [[southWestDict objectForKey:@"lat"] doubleValue];
- CLLocationDegrees latitudeDelta = fabs(northEastLatitude - southWestLatitude);
- CLLocationDegrees northEastLongitude = [[northEastDict objectForKey:@"lng"] doubleValue];
- CLLocationDegrees southWestLongitude = [[southWestDict objectForKey:@"lng"] doubleValue];
- CLLocationDegrees longitudeDelta = fabs(northEastLongitude - southWestLongitude);
- MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
- placemark.region = MKCoordinateRegionMake(placemark.location.coordinate, span);
-
+ SVPlacemark *placemark = [[SVPlacemark alloc] initWithDictionary:result];
[placemarks addObject:placemark];
}];
}
View
31 SVGeocoder/SVPlacemark.h
@@ -13,20 +13,23 @@
@interface SVPlacemark : NSObject
-@property (nonatomic, strong) NSString *formattedAddress;
-@property (nonatomic, strong) NSString *subThoroughfare;
-@property (nonatomic, strong) NSString *thoroughfare;
-@property (nonatomic, strong) NSString *subLocality;
-@property (nonatomic, strong) NSString *locality;
-@property (nonatomic, strong) NSString *subAdministrativeArea;
-@property (nonatomic, strong) NSString *administrativeArea;
-@property (nonatomic, strong) NSString *administrativeAreaCode;
-@property (nonatomic, strong) NSString *postalCode;
-@property (nonatomic, strong) NSString *country;
-@property (nonatomic, strong) NSString *ISOcountryCode;
+- (id)initWithDictionary:(NSDictionary*)dictionary;
-@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
-@property (nonatomic, readwrite) MKCoordinateRegion region;
-@property (nonatomic, strong) CLLocation *location;
+@property (nonatomic, strong, readonly) NSString *name;
+@property (nonatomic, strong, readonly) NSString *formattedAddress;
+@property (nonatomic, strong, readonly) NSString *subThoroughfare;
+@property (nonatomic, strong, readonly) NSString *thoroughfare;
+@property (nonatomic, strong, readonly) NSString *subLocality;
+@property (nonatomic, strong, readonly) NSString *locality;
+@property (nonatomic, strong, readonly) NSString *subAdministrativeArea;
+@property (nonatomic, strong, readonly) NSString *administrativeArea;
+@property (nonatomic, strong, readonly) NSString *administrativeAreaCode;
+@property (nonatomic, strong, readonly) NSString *postalCode;
+@property (nonatomic, strong, readonly) NSString *country;
+@property (nonatomic, strong, readonly) NSString *ISOcountryCode;
+
+@property (nonatomic, readonly) CLLocationCoordinate2D coordinate;
+@property (nonatomic, readonly) MKCoordinateRegion region;
+@property (nonatomic, strong, readonly) CLLocation *location;
@end
View
98 SVGeocoder/SVPlacemark.m
@@ -10,12 +10,108 @@
#import "SVPlacemark.h"
+@interface SVPlacemark ()
+
+@property (nonatomic, strong, readwrite) NSString *formattedAddress;
+@property (nonatomic, strong, readwrite) NSString *subThoroughfare;
+@property (nonatomic, strong, readwrite) NSString *thoroughfare;
+@property (nonatomic, strong, readwrite) NSString *subLocality;
+@property (nonatomic, strong, readwrite) NSString *locality;
+@property (nonatomic, strong, readwrite) NSString *subAdministrativeArea;
+@property (nonatomic, strong, readwrite) NSString *administrativeArea;
+@property (nonatomic, strong, readwrite) NSString *administrativeAreaCode;
+@property (nonatomic, strong, readwrite) NSString *postalCode;
+@property (nonatomic, strong, readwrite) NSString *country;
+@property (nonatomic, strong, readwrite) NSString *ISOcountryCode;
+
+@property (nonatomic, readwrite) CLLocationCoordinate2D coordinate;
+@property (nonatomic, readwrite) MKCoordinateRegion region;
+@property (nonatomic, strong, readwrite) CLLocation *location;
+
+@end
+
@implementation SVPlacemark
@synthesize formattedAddress, subThoroughfare, thoroughfare, subLocality, locality, subAdministrativeArea, administrativeArea, administrativeAreaCode, postalCode, country, ISOcountryCode, coordinate, location, region;
-- (NSString*)description {
+- (id)initWithDictionary:(NSDictionary *)result {
+ self.formattedAddress = [result objectForKey:@"formatted_address"];
+
+ NSArray *addressComponents = [result objectForKey:@"address_components"];
+
+ [addressComponents enumerateObjectsUsingBlock:^(NSDictionary *component, NSUInteger idx, BOOL *stopAddress) {
+ NSArray *types = [component objectForKey:@"types"];
+
+ if([types containsObject:@"street_number"])
+ self.subThoroughfare = [component objectForKey:@"long_name"];
+
+ if([types containsObject:@"route"])
+ self.thoroughfare = [component objectForKey:@"long_name"];
+
+ if([types containsObject:@"administrative_area_level_3"] || [types containsObject:@"sublocality"] || [types containsObject:@"neighborhood"])
+ self.subLocality = [component objectForKey:@"long_name"];
+
+ if([types containsObject:@"locality"])
+ self.locality = [component objectForKey:@"long_name"];
+
+ if([types containsObject:@"administrative_area_level_2"])
+ self.subAdministrativeArea = [component objectForKey:@"long_name"];
+
+ if([types containsObject:@"administrative_area_level_1"]) {
+ self.administrativeArea = [component objectForKey:@"long_name"];
+ self.administrativeAreaCode = [component objectForKey:@"short_name"];
+ }
+
+ if([types containsObject:@"country"]) {
+ self.country = [component objectForKey:@"long_name"];
+ self.ISOcountryCode = [component objectForKey:@"short_name"];
+ }
+
+ if([types containsObject:@"postal_code"])
+ self.postalCode = [component objectForKey:@"long_name"];
+
+ }];
+
+ NSDictionary *locationDict = [[result objectForKey:@"geometry"] objectForKey:@"location"];
+ NSDictionary *boundsDict = [[result objectForKey:@"geometry"] objectForKey:@"bounds"];
+
+ CLLocationDegrees lat = [[locationDict objectForKey:@"lat"] doubleValue];
+ CLLocationDegrees lng = [[locationDict objectForKey:@"lng"] doubleValue];
+ self.coordinate = CLLocationCoordinate2DMake(lat, lng);
+ self.location = [[CLLocation alloc] initWithLatitude:lat longitude:lng];
+
+ NSDictionary *northEastDict = [boundsDict objectForKey:@"northeast"];
+ NSDictionary *southWestDict = [boundsDict objectForKey:@"southwest"];
+ CLLocationDegrees northEastLatitude = [[northEastDict objectForKey:@"lat"] doubleValue];
+ CLLocationDegrees southWestLatitude = [[southWestDict objectForKey:@"lat"] doubleValue];
+ CLLocationDegrees latitudeDelta = fabs(northEastLatitude - southWestLatitude);
+ CLLocationDegrees northEastLongitude = [[northEastDict objectForKey:@"lng"] doubleValue];
+ CLLocationDegrees southWestLongitude = [[southWestDict objectForKey:@"lng"] doubleValue];
+ CLLocationDegrees longitudeDelta = fabs(northEastLongitude - southWestLongitude);
+ MKCoordinateSpan span = MKCoordinateSpanMake(latitudeDelta, longitudeDelta);
+ self.region = MKCoordinateRegionMake(self.location.coordinate, span);
+
+ return self;
+}
+
+- (NSString *)name {
+ if(self.subThoroughfare && self.thoroughfare)
+ return [NSString stringWithFormat:@"%@ %@", self.subThoroughfare, self.thoroughfare];
+ else if(self.thoroughfare)
+ return self.thoroughfare;
+ else if(self.subLocality)
+ return self.subLocality;
+ else if(self.locality)
+ return [NSString stringWithFormat:@"%@, %@", self.locality, self.administrativeAreaCode];
+ else if(self.administrativeArea)
+ return self.administrativeArea;
+ else if(self.country)
+ return self.country;
+ return nil;
+}
+
+- (NSString*)description {
NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
formattedAddress, @"formattedAddress",
subThoroughfare?subThoroughfare:[NSNull null], @"subThoroughfare",

0 comments on commit caccd2c

Please sign in to comment.
Something went wrong with that request. Please try again.