Skip to content

Commit 440776f

Browse files
committed
added support for region parameters when geocoding address string
1 parent b594ed8 commit 440776f

File tree

3 files changed

+27
-5
lines changed

3 files changed

+27
-5
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
@@ -31,7 +31,7 @@ public String getName() {
3131
}
3232

3333
@ReactMethod
34-
public void geocodeAddress(String addressName, String language, Promise promise) {
34+
public void geocodeAddress(String addressName, float swLat, float swLng, float neLat, float neLng, String language, Promise promise) {
3535
if (geocoder == null) {
3636
geocoder = new Geocoder(getReactApplicationContext(), new Locale(language));
3737
}
@@ -42,7 +42,7 @@ public void geocodeAddress(String addressName, String language, Promise promise)
4242
}
4343

4444
try {
45-
List<Address> addresses = geocoder.getFromLocationName(addressName, 2);
45+
List<Address> addresses = geocoder.getFromLocationName(addressName, 2, swLat, swLng, neLat, neLng);
4646
if(addresses != null && addresses.size() > 0) {
4747
promise.resolve(transform(addresses));
4848
} else {

ios/RNGeocoder/RNGeocoder.m

Lines changed: 16 additions & 1 deletion
Original file line numberDiff line numberDiff line change
@@ -55,6 +55,10 @@ @implementation RNGeocoder
5555
}
5656

5757
RCT_EXPORT_METHOD(geocodeAddress:(NSString *)address
58+
withSWLat:(CGFloat)swLat
59+
withSWLng:(CGFloat)swLng
60+
withNELat:(CGFloat)neLat
61+
withNELng:(CGFloat)neLng
5862
language:(NSString *)language
5963
resolver:(RCTPromiseResolveBlock)resolve
6064
rejecter:(RCTPromiseRejectBlock)reject)
@@ -68,6 +72,17 @@ @implementation RNGeocoder
6872
}
6973

7074
CLGeocodeCompletionHandler handler = ^void(NSArray< CLPlacemark *> *placemarks, NSError *error) {
75+
CLRegion* region;
76+
if (swLat == 0 || swLng == 0 || neLat == 0 || neLng == 0){
77+
region = nil;
78+
}else{
79+
CLLocationCoordinate2D center = CLLocationCoordinate2DMake(swLat + (neLat-swLat) / 2, swLng + (neLng-swLng) / 2);
80+
//Computing the radius based on lat delta, since 1 lat = 111 km no matter the location
81+
float latDelta = neLat - swLat;
82+
float radiusLat = (latDelta/2);
83+
float radius = radiusLat * 111000;
84+
region = [[CLCircularRegion alloc] initWithCenter:center radius:radius identifier:@"Search Radius"];
85+
}
7186
if (error) {
7287
if (placemarks.count == 0) {
7388
return reject(@"NOT_FOUND", @"geocodeAddress failed", error);
@@ -78,7 +93,7 @@ @implementation RNGeocoder
7893
};
7994

8095
if (@available(iOS 11.0, *)) {
81-
[self.geocoder geocodeAddressString:address inRegion:nil preferredLocale:[NSLocale localeWithLocaleIdentifier:language] completionHandler:handler];
96+
[self.geocoder geocodeAddressString:address inRegion:region preferredLocale:[NSLocale localeWithLocaleIdentifier:language] completionHandler:handler];
8297
} else {
8398
[self.geocoder geocodeAddressString:address completionHandler:handler];
8499
}

src/geocoder.js

Lines changed: 9 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -38,16 +38,23 @@ export default {
3838
});
3939
},
4040

41-
geocodeAddress(address) {
41+
geocodeAddress(address, swLat, swLng, neLat, neLng) {
4242
if (!address) {
4343
return Promise.reject(new Error("Address is required"));
4444
}
4545

4646
if (this.useGoogleOnIos && this.apiKey && Platform.OS === 'ios') {
4747
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
4848
}
49+
// If any of the parameters is not set for the region, we set them all to 0
50+
if (swLat == null || swLng == null || neLat == null || neLng == null){
51+
swLat = 0;
52+
swLng = 0;
53+
neLat = 0;
54+
neLng = 0;
55+
}
4956

50-
return RNGeocoder.geocodeAddress(address, this.language).catch(err => {
57+
return RNGeocoder.geocodeAddress(address, swLat, swLng, neLat, neLng, this.language).catch(err => {
5158
if (!this.apiKey) { throw err; }
5259
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
5360
});

0 commit comments

Comments
 (0)