Skip to content

Commit b3568eb

Browse files
committed
Merge branch 'master' of https://github.com/androidseb/react-native-geocoder-reborn into androidseb-master
2 parents 9b63c03 + 440776f commit b3568eb

File tree

3 files changed

+25
-3
lines changed

3 files changed

+25
-3
lines changed

android/src/main/java/dev/timwang/RNGeocoder/RNGeocoderModule.java

Lines changed: 2 additions & 2 deletions
Original file line numberDiff line numberDiff line change
@@ -42,9 +42,9 @@ public void init(String locale, Integer maxResults) {
4242
}
4343

4444
@ReactMethod
45-
public void geocodeAddress(String addressName, String language, Promise promise) {
45+
public void geocodeAddress(String addressName, float swLat, float swLng, float neLat, float neLng, String language, Promise promise) {
4646
try {
47-
List<Address> addresses = geocoder.getFromLocationName(addressName, maxResults);
47+
List<Address> addresses = geocoder.getFromLocationName(addressName, 2, swLat, swLng, neLat, neLng, maxResults);
4848
if(addresses != null && addresses.size() > 0) {
4949
promise.resolve(transform(addresses));
5050
} 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: 7 additions & 0 deletions
Original file line numberDiff line numberDiff line change
@@ -94,6 +94,13 @@ export default {
9494
if (this._forceGoogleOnIos && Platform.OS === 'ios') {
9595
return this.geocodeAddressGoogle(address);
9696
}
97+
// If any of the parameters is not set for the region, we set them all to 0
98+
if (swLat == null || swLng == null || neLat == null || neLng == null){
99+
swLat = 0;
100+
swLng = 0;
101+
neLat = 0;
102+
neLng = 0;
103+
}
97104

98105
if (typeof RNGeocoder === 'undefined') {
99106
if (!this._fallbackToGoogle) {

0 commit comments

Comments
 (0)