|
10 | 10 |
|
11 | 11 | #import "SVPlacemark.h"
|
12 | 12 |
|
| 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 | + |
13 | 33 |
|
14 | 34 | @implementation SVPlacemark
|
15 | 35 |
|
16 | 36 | @synthesize formattedAddress, subThoroughfare, thoroughfare, subLocality, locality, subAdministrativeArea, administrativeArea, administrativeAreaCode, postalCode, country, ISOcountryCode, coordinate, location, region;
|
17 | 37 |
|
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 { |
19 | 115 | NSDictionary *dict = [NSDictionary dictionaryWithObjectsAndKeys:
|
20 | 116 | formattedAddress, @"formattedAddress",
|
21 | 117 | subThoroughfare?subThoroughfare:[NSNull null], @"subThoroughfare",
|
|
0 commit comments