forked from TextureGroup/Texture
-
Notifications
You must be signed in to change notification settings - Fork 0
Expand file tree
/
Copy pathLocationModel.m
More file actions
146 lines (116 loc) · 5.13 KB
/
Copy pathLocationModel.m
File metadata and controls
146 lines (116 loc) · 5.13 KB
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
//
// LocationModel.m
// Sample
//
// Created by Hannah Troisi on 2/26/16.
//
// Copyright (c) 2014-present, Facebook, Inc. All rights reserved.
// This source code is licensed under the BSD-style license found in the
// LICENSE file in the root directory of this source tree. An additional grant
// of patent rights can be found in the PATENTS file in the same directory.
//
// THE SOFTWARE IS PROVIDED "AS IS", WITHOUT WARRANTY OF ANY KIND, EXPRESS OR
// IMPLIED, INCLUDING BUT NOT LIMITED TO THE WARRANTIES OF MERCHANTABILITY,
// FITNESS FOR A PARTICULAR PURPOSE AND NONINFRINGEMENT. IN NO EVENT SHALL
// FACEBOOK BE LIABLE FOR ANY CLAIM, DAMAGES OR OTHER LIABILITY, WHETHER IN AN
// ACTION OF CONTRACT, TORT OR OTHERWISE, ARISING FROM, OUT OF OR IN
// CONNECTION WITH THE SOFTWARE OR THE USE OR OTHER DEALINGS IN THE SOFTWARE.
//
#import "LocationModel.h"
#import <CoreLocation/CLGeocoder.h>
@implementation LocationModel
{
BOOL _placemarkFetchInProgress;
void (^_placemarkCallbackBlock)(LocationModel *);
}
#pragma mark - Lifecycle
- (nullable instancetype)initWith500pxPhoto:(NSDictionary *)dictionary
{
NSNumber *latitude = [dictionary objectForKey:@"latitude"];
NSNumber *longitude = [dictionary objectForKey:@"longitude"];
// early return if location is "<null>"
if (![latitude isKindOfClass:[NSNumber class]] || ![longitude isKindOfClass:[NSNumber class]]) {
return nil;
}
self = [super init];
if (self) {
// set coordiantes
_coordinates = CLLocationCoordinate2DMake([latitude floatValue], [longitude floatValue]);
// get CLPlacemark with MKReverseGeocoder
[self beginReverseGeocodingLocationFromCoordinates];
}
return self;
}
#pragma mark - Instance Methods
// return location placemark if fetched, else set completion block for fetch finish
- (void)reverseGeocodedLocationWithCompletionBlock:(void (^)(LocationModel *))blockName
{
if (_placemark) {
// call block if placemark already fetched
if (blockName) {
blockName(self);
}
} else {
// set placemark reverse geocoding completion block
_placemarkCallbackBlock = blockName;
// if fetch not in progress, begin
if (!_placemarkFetchInProgress) {
[self beginReverseGeocodingLocationFromCoordinates];
}
}
}
#pragma mark - Helper Methods
- (void)beginReverseGeocodingLocationFromCoordinates
{
if (_placemarkFetchInProgress) {
return;
}
_placemarkFetchInProgress = YES;
CLLocation *location = [[CLLocation alloc] initWithLatitude:_coordinates.latitude longitude:_coordinates.longitude];
CLGeocoder *geocoder = [[CLGeocoder alloc] init];
[geocoder reverseGeocodeLocation:location completionHandler:^(NSArray<CLPlacemark *> * _Nullable placemarks, NSError * _Nullable error) {
// completion handler gets called on main thread
_placemark = [placemarks lastObject];
_locationString = [self locationStringFromCLPlacemark];
// check if completion block set, call it - DO NOT CALL A NIL BLOCK!
if (_placemarkCallbackBlock) {
// call the block with arguments
_placemarkCallbackBlock(self);
}
}];
}
- (nullable NSString *)locationStringFromCLPlacemark
{
// early return if no location info
if (!_placemark)
{
return nil;
}
// @property (nonatomic, readonly, copy, nullable) NSString *name; // eg. Apple Inc.
// @property (nonatomic, readonly, copy, nullable) NSString *thoroughfare; // street name, eg. Infinite Loop
// @property (nonatomic, readonly, copy, nullable) NSString *subThoroughfare; // eg. 1
// @property (nonatomic, readonly, copy, nullable) NSString *locality; // city, eg. Cupertino
// @property (nonatomic, readonly, copy, nullable) NSString *subLocality; // neighborhood, common name, eg. Mission District
// @property (nonatomic, readonly, copy, nullable) NSString *administrativeArea; // state, eg. CA
// @property (nonatomic, readonly, copy, nullable) NSString *subAdministrativeArea; // county, eg. Santa Clara
// @property (nonatomic, readonly, copy, nullable) NSString *postalCode; // zip code, eg. 95014
// @property (nonatomic, readonly, copy, nullable) NSString *ISOcountryCode; // eg. US
// @property (nonatomic, readonly, copy, nullable) NSString *country; // eg. United States
// @property (nonatomic, readonly, copy, nullable) NSString *inlandWater; // eg. Lake Tahoe
// @property (nonatomic, readonly, copy, nullable) NSString *ocean; // eg. Pacific Ocean
// @property (nonatomic, readonly, copy, nullable) NSArray<NSString *> *areasOfInterest; // eg. Golden Gate Park
NSString *locationString;
if (_placemark.inlandWater) {
locationString = _placemark.inlandWater;
} else if (_placemark.subLocality && _placemark.locality) {
locationString = [NSString stringWithFormat:@"%@, %@", _placemark.subLocality, _placemark.locality];
} else if (_placemark.administrativeArea && _placemark.subAdministrativeArea) {
locationString = [NSString stringWithFormat:@"%@, %@", _placemark.subAdministrativeArea, _placemark.administrativeArea];
} else if (_placemark.country) {
locationString = _placemark.country;
} else {
locationString = @"ERROR";
}
return locationString;
}
@end