-
-
Notifications
You must be signed in to change notification settings - Fork 85
Expand file tree
/
Copy pathGeocodingHandler.m
More file actions
98 lines (85 loc) · 3.39 KB
/
GeocodingHandler.m
File metadata and controls
98 lines (85 loc) · 3.39 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
//
// GeocodingHandler.m
// geocoding
//
// Created by Maurits van Beusekom on 07/06/2020.
//
#import "GeocodingHandler.h"
@implementation GeocodingHandler {
CLGeocoder* _geocoder;
}
- (id)init {
self = [super init];
if (self) {
_geocoder = [[CLGeocoder alloc] init];
}
return self;
}
- (void) geocodeFromAddress: (NSString *)address
locale: (NSLocale *)locale
sLat: (CGFloat) sLat
wLng: (CGFloat) sLng
nLat: (CGFloat) nLat
eLng: (CGFloat) nLng
success: (GeocodingSuccess)successHandler
failure: (GeocodingFailure)failureHandler {
if (address == nil) {
failureHandler(@"ARGUMENT_ERROR", @"Please supply a valid string containing the address to lookup");
return;
}
CLRegion* region;
if (sLat == 0 || sLng == 0 || nLat == 0 || nLng == 0){
region = nil;
}else{
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(sLat + (nLat-sLat) / 2, sLng + (nLng-sLng) / 2);
//Computing the radius based on lat delta, since 1 lat = 111 km no matter the location
float latDelta = nLat - sLat;
float radiusLat = (latDelta/2);
float radius = radiusLat * 111000;
region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"Search Radius"];
}
[_geocoder geocodeAddressString:address
inRegion:region
preferredLocale:locale
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error)
{
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];
}];
return;
}
- (void) geocodeToAddress: (CLLocation *)location
locale: (NSLocale *)locale
success: (GeocodingSuccess)successHandler
failure: (GeocodingFailure)failureHandler {
[_geocoder reverseGeocodeLocation:location
preferredLocale:locale
completionHandler:^(NSArray< CLPlacemark *> *__nullable placemarks, NSError *__nullable error) {
[GeocodingHandler completeGeocodingWith:placemarks
error:error
success:successHandler
failure:failureHandler];
}];
}
+ (void) completeGeocodingWith: (NSArray<CLPlacemark *> *) placemarks
error: (NSError *) error
success: (GeocodingSuccess)successHandler
failure: (GeocodingFailure) failureHandler {
if (error != nil) {
if(error.code == kCLErrorGeocodeFoundNoResult){
failureHandler(@"NOT_FOUND", @"Could not find any result for the supplied address or coordinates.");
} else {
failureHandler(@"IO_ERROR", error.description);
}
} else if (placemarks == nil) {
failureHandler(@"NOT_FOUND", @"Could not find any result for the supplied address or coordinates.");
} else {
successHandler(placemarks);
}
}
+ (NSString *) languageCode:(NSLocale *)locale {
return [locale languageCode];
}
@end