This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathRNGeocoder.m
More file actions
140 lines (102 loc) · 4.15 KB
/
RNGeocoder.m
File metadata and controls
140 lines (102 loc) · 4.15 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
#import "RNGeocoder.h"
#import <CoreLocation/CoreLocation.h>
#import <React/RCTConvert.h>
@implementation RCTConvert (CoreLocation)
+ (CLLocation *)CLLocation:(id)json
{
json = [self NSDictionary:json];
double lat = [RCTConvert double:json[@"lat"]];
double lng = [RCTConvert double:json[@"lng"]];
return [[CLLocation alloc] initWithLatitude:lat longitude:lng];
}
@end
@implementation RNGeocoder
RCT_EXPORT_MODULE();
RCT_EXPORT_METHOD(geocodePosition:(CLLocation *)location
NSString: locale
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
if (self.geocoder.geocoding) {
[self.geocoder cancelGeocode];
}
NSLocale *geocoderLocale = [[NSLocale alloc] initWithLocaleIdentifier:locale];
if (@available(iOS 11.0, *)) {
[self.geocoder reverseGeocodeLocation:location preferredLocale:geocoderLocale completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
if (placemarks.count == 0) {
return reject(@"NOT_FOUND", @"geocodePosition failed", error);
}
return reject(@"ERROR", @"geocodePosition failed", error);
}
resolve([self placemarksToDictionary:placemarks]);
}];
} else {
[self.geocoder reverseGeocodeLocation:location completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
if (placemarks.count == 0) {
return reject(@"NOT_FOUND", @"geocodePosition failed", error);
}
return reject(@"ERROR", @"geocodePosition failed", error);
}
resolve([self placemarksToDictionary:placemarks]);
}];
}
}
RCT_EXPORT_METHOD(geocodeAddress:(NSString *)address
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
{
if (!self.geocoder) {
self.geocoder = [[CLGeocoder alloc] init];
}
if (self.geocoder.geocoding) {
[self.geocoder cancelGeocode];
}
[self.geocoder geocodeAddressString:address completionHandler:^(NSArray *placemarks, NSError *error) {
if (error) {
if (placemarks.count == 0) {
return reject(@"NOT_FOUND", @"geocodeAddress failed", error);
}
return reject(@"ERROR", @"geocodeAddress failed", error);
}
resolve([self placemarksToDictionary:placemarks]);
}];
}
- (NSArray *)placemarksToDictionary:(NSArray *)placemarks {
NSMutableArray *results = [[NSMutableArray alloc] init];
for (int i = 0; i < placemarks.count; i++) {
CLPlacemark* placemark = [placemarks objectAtIndex:i];
NSString* name = [NSNull null];
if (![placemark.name isEqualToString:placemark.locality] &&
![placemark.name isEqualToString:placemark.thoroughfare] &&
![placemark.name isEqualToString:placemark.subThoroughfare])
{
name = placemark.name;
}
NSArray *lines = placemark.addressDictionary[@"FormattedAddressLines"];
NSDictionary *result = @{
@"feature": name,
@"position": @{
@"lat": [NSNumber numberWithDouble:placemark.location.coordinate.latitude],
@"lng": [NSNumber numberWithDouble:placemark.location.coordinate.longitude],
},
@"country": placemark.country ?: [NSNull null],
@"countryCode": placemark.ISOcountryCode ?: [NSNull null],
@"locality": placemark.locality ?: [NSNull null],
@"subLocality": placemark.subLocality ?: [NSNull null],
@"streetName": placemark.thoroughfare ?: [NSNull null],
@"streetNumber": placemark.subThoroughfare ?: [NSNull null],
@"postalCode": placemark.postalCode ?: [NSNull null],
@"adminArea": placemark.administrativeArea ?: [NSNull null],
@"subAdminArea": placemark.subAdministrativeArea ?: [NSNull null],
@"formattedAddress": [lines componentsJoinedByString:@", "] ?: [NSNull null]
};
[results addObject:result];
}
return results;
}
@end