@@ -10,8 +10,8 @@ + (CLLocation *)CLLocation:(id)json
1010{
1111 json = [self NSDictionary: json];
1212
13- double lat = [RCTConvert double: json[@" latitude " ]];
14- double lng = [RCTConvert double: json[@" longitude " ]];
13+ double lat = [RCTConvert double: json[@" lat " ]];
14+ double lng = [RCTConvert double: json[@" lng " ]];
1515 return [[CLLocation alloc ] initWithLatitude: lat longitude: lng];
1616}
1717
@@ -22,7 +22,9 @@ @implementation RNGeocoder
2222
2323RCT_EXPORT_MODULE ();
2424
25- RCT_EXPORT_METHOD (reverseGeocodeLocation:(CLLocation *)location errorCallback: (RCTResponseSenderBlock)errorCallback successCallback: (RCTResponseSenderBlock)successCallback)
25+ RCT_EXPORT_METHOD (geocodePosition:(CLLocation *)location
26+ resolver:(RCTPromiseResolveBlock)resolve
27+ rejecter:(RCTPromiseRejectBlock)reject)
2628{
2729 if (!self.geocoder ) {
2830 self.geocoder = [[CLGeocoder alloc ] init ];
@@ -34,38 +36,38 @@ @implementation RNGeocoder
3436
3537 if (error) {
3638 if (placemarks.count == 0 ) {
37- return errorCallback (@[ @" Not found " ] );
39+ return reject ( @" NOT_FOUND " , @" geocodePosition failed " , error );
3840 }
3941
40- return errorCallback (@[ error.description] );
42+ return reject ( @" ERROR " , @" geocodePosition failed " , error);
4143 }
4244
43-
44- successCallback (@[[self placemarksToDictionary: placemarks]]);
45+ resolve ([self placemarksToDictionary: placemarks]);
4546
4647 }];
4748}
4849
49- RCT_EXPORT_METHOD (geocodeAddress:(NSString *)address errorCallback: (RCTResponseSenderBlock)errorCallback successCallback: (RCTResponseSenderBlock)successCallback)
50+ RCT_EXPORT_METHOD (geocodeAddress:(NSString *)address
51+ resolver:(RCTPromiseResolveBlock)resolve
52+ rejecter:(RCTPromiseRejectBlock)reject)
5053{
51- if (!self.geocoder ) {
52- self.geocoder = [[CLGeocoder alloc ] init ];
53- }
54-
55- [self .geocoder cancelGeocode ];
54+ if (!self.geocoder ) {
55+ self.geocoder = [[CLGeocoder alloc ] init ];
56+ }
5657
57- [self .geocoder geocodeAddressString: address completionHandler: ^( NSArray *placemarks, NSError *error) {
58+ [self .geocoder cancelGeocode ];
5859
59- if (error) {
60- if (placemarks.count == 0 ) {
61- return errorCallback (@[@" Not found" ]);
62- }
60+ [self .geocoder geocodeAddressString: address completionHandler: ^(NSArray *placemarks, NSError *error) {
6361
64- return errorCallback (@[error.description]);
65- }
62+ if (error) {
63+ if (placemarks.count == 0 ) {
64+ return reject (@" NOT_FOUND" , @" geocodePosition failed" , error);
65+ }
6666
67- successCallback (@[[self placemarksToDictionary: placemarks]]);
67+ return reject (@" ERROR" , @" geocodePosition failed" , error);
68+ }
6869
70+ resolve ([self placemarksToDictionary: placemarks]);
6971 }];
7072}
7173
@@ -76,20 +78,34 @@ - (NSArray *)placemarksToDictionary:(NSArray *)placemarks {
7678 for (int i = 0 ; i < placemarks.count ; i++) {
7779 CLPlacemark* placemark = [placemarks objectAtIndex: i];
7880
81+ NSString * name = [NSNull null ];
82+
83+ if (![placemark.name isEqualToString: placemark.locality] &&
84+ ![placemark.name isEqualToString: placemark.thoroughfare] &&
85+ ![placemark.name isEqualToString: placemark.subThoroughfare])
86+ {
87+
88+ name = placemark.name ;
89+ }
90+
91+ NSArray *lines = placemark.addressDictionary [@" FormattedAddressLines" ];
92+
7993 NSDictionary *result = @{
80- @" name " : placemark. name ?: [ NSNull null ] ,
94+ @" feature " : name,
8195 @" position" : @{
8296 @" lat" : [NSNumber numberWithDouble: placemark.location.coordinate.latitude],
8397 @" lng" : [NSNumber numberWithDouble: placemark.location.coordinate.longitude],
8498 },
8599 @" country" : placemark.country ?: [NSNull null ],
100+ @" countryCode" : placemark.ISOcountryCode ?: [NSNull null ],
86101 @" locality" : placemark.locality ?: [NSNull null ],
87102 @" subLocality" : placemark.subLocality ?: [NSNull null ],
88- @" thoroughfare " : placemark.thoroughfare ?: [NSNull null ],
89- @" subThoroughfare " : placemark.subThoroughfare ?: [NSNull null ],
103+ @" streetName " : placemark.thoroughfare ?: [NSNull null ],
104+ @" streetNumber " : placemark.subThoroughfare ?: [NSNull null ],
90105 @" postalCode" : placemark.postalCode ?: [NSNull null ],
91- @" administrativeArea" : placemark.administrativeArea ?: [NSNull null ],
92- @" subAdministrativeArea" : placemark.subAdministrativeArea ?: [NSNull null ],
106+ @" adminArea" : placemark.administrativeArea ?: [NSNull null ],
107+ @" subAdminArea" : placemark.subAdministrativeArea ?: [NSNull null ],
108+ @" formattedAddress" : [lines componentsJoinedByString: @" ," ]
93109 };
94110
95111 [results addObject: result];
0 commit comments