Skip to content
This repository was archived by the owner on Sep 4, 2021. It is now read-only.

Commit a5d8c35

Browse files
committed
feature parity between iOS / android
1 parent 254ae43 commit a5d8c35

File tree

4 files changed

+61
-62
lines changed

4 files changed

+61
-62
lines changed

android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -33,7 +33,7 @@ public String getName() {
3333
@ReactMethod
3434
public void geocodeAddress(String addressName, Promise promise) {
3535
if (!geocoder.isPresent()) {
36-
promise.reject("-1", "Geocoder not available for this platform");
36+
promise.reject("NOT_AVAILABLE", "Geocoder not available for this platform");
3737
return;
3838
}
3939

@@ -50,7 +50,7 @@ public void geocodeAddress(String addressName, Promise promise) {
5050
@ReactMethod
5151
public void geocodePosition(ReadableMap position, Promise promise) {
5252
if (!geocoder.isPresent()) {
53-
promise.reject("-1", "Geocoder not available for this platform");
53+
promise.reject("NOT_AVAILABLE", "Geocoder not available for this platform");
5454
return;
5555
}
5656

index.js

Lines changed: 15 additions & 32 deletions
Original file line numberDiff line numberDiff line change
@@ -1,39 +1,22 @@
1-
var {
2-
NativeModules: {
3-
RNGeocoder
4-
}
5-
} = require('react-native');
1+
import { NativeModules } from 'react-native';
62

7-
var Geocoder = {
3+
const { RNGeocoder } = NativeModules;
84

9-
reverseGeocodeLocation: function(location, callback) {
5+
export default {
106

11-
return new Promise((resolve, reject) => {
7+
geocodePosition(position) {
8+
if (!position || !position.lat || !position.lng) {
9+
return Promise.reject(new Error("invalid position: {lat, lng} required"));
10+
}
1211

13-
RNGeocoder.reverseGeocodeLocation(location, (err) => {
14-
callback && callback(err, null);
15-
reject(err);
16-
}, (data) => {
17-
callback && callback(null, data);
18-
resolve(data);
19-
});
20-
});
12+
return RNGeocoder.geocodePosition(position);
2113
},
2214

23-
geocodeAddress: function(address, callback) {
15+
geocodeAddress(address) {
16+
if (!address) {
17+
return Promise.reject(new Error("address is null"));
18+
}
2419

25-
return new Promise((resolve, reject) => {
26-
27-
RNGeocoder.geocodeAddress(address, (err) => {
28-
callback && callback(err, null);
29-
reject(err);
30-
}, (data) => {
31-
callback && callback(null, data);
32-
resolve(data);
33-
});
34-
});
35-
}
36-
};
37-
38-
39-
module.exports = Geocoder;
20+
return RNGeocoder.geocodeAddress(address);
21+
},
22+
}

ios/RNGeocoder.xcodeproj/project.pbxproj

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -212,7 +212,7 @@
212212
BF8FCF9E1BFFB184000FCD37 /* Debug */ = {
213213
isa = XCBuildConfiguration;
214214
buildSettings = {
215-
HEADER_SEARCH_PATHS = "$(SRCROOT)/../react-native/React/**";
215+
HEADER_SEARCH_PATHS = "$(SRCROOT)/../node_modules/react-native/React/**";
216216
OTHER_LDFLAGS = "-ObjC";
217217
PRODUCT_NAME = "$(TARGET_NAME)";
218218
SKIP_INSTALL = YES;
@@ -222,7 +222,7 @@
222222
BF8FCF9F1BFFB184000FCD37 /* Release */ = {
223223
isa = XCBuildConfiguration;
224224
buildSettings = {
225-
HEADER_SEARCH_PATHS = "$(SRCROOT)/../react-native/React/**";
225+
HEADER_SEARCH_PATHS = "$(SRCROOT)/../node_modules/react-native/React/**";
226226
OTHER_LDFLAGS = "-ObjC";
227227
PRODUCT_NAME = "$(TARGET_NAME)";
228228
SKIP_INSTALL = YES;

ios/RNGeocoder/RNGeocoder.m

Lines changed: 42 additions & 26 deletions
Original file line numberDiff line numberDiff line change
@@ -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

2323
RCT_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

Comments
 (0)