Skip to content

added support for region parameters when geocoding address string #9

New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Merged
merged 1 commit into from
Mar 15, 2020
Merged
Show file tree
Hide file tree
Changes from all commits
Commits
File filter

Filter by extension

Filter by extension

Conversations
Failed to load comments.
Loading
Jump to
Jump to file
Failed to load files.
Loading
Diff view
Diff view
Original file line number Diff line number Diff line change
Expand Up @@ -31,7 +31,7 @@ public String getName() {
}

@ReactMethod
public void geocodeAddress(String addressName, String language, Promise promise) {
public void geocodeAddress(String addressName, float swLat, float swLng, float neLat, float neLng, String language, Promise promise) {
if (geocoder == null) {
geocoder = new Geocoder(getReactApplicationContext(), new Locale(language));
}
Expand All @@ -42,7 +42,7 @@ public void geocodeAddress(String addressName, String language, Promise promise)
}

try {
List<Address> addresses = geocoder.getFromLocationName(addressName, 2);
List<Address> addresses = geocoder.getFromLocationName(addressName, 2, swLat, swLng, neLat, neLng);
if(addresses != null && addresses.size() > 0) {
promise.resolve(transform(addresses));
} else {
Expand Down
17 changes: 16 additions & 1 deletion ios/RNGeocoder/RNGeocoder.m
Original file line number Diff line number Diff line change
Expand Up @@ -55,6 +55,10 @@ @implementation RNGeocoder
}

RCT_EXPORT_METHOD(geocodeAddress:(NSString *)address
withSWLat:(CGFloat)swLat
withSWLng:(CGFloat)swLng
withNELat:(CGFloat)neLat
withNELng:(CGFloat)neLng
language:(NSString *)language
resolver:(RCTPromiseResolveBlock)resolve
rejecter:(RCTPromiseRejectBlock)reject)
Expand All @@ -68,6 +72,17 @@ @implementation RNGeocoder
}

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

if (@available(iOS 11.0, *)) {
[self.geocoder geocodeAddressString:address inRegion:nil preferredLocale:[NSLocale localeWithLocaleIdentifier:language] completionHandler:handler];
[self.geocoder geocodeAddressString:address inRegion:region preferredLocale:[NSLocale localeWithLocaleIdentifier:language] completionHandler:handler];
} else {
[self.geocoder geocodeAddressString:address completionHandler:handler];
}
Expand Down
11 changes: 9 additions & 2 deletions src/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -38,16 +38,23 @@ export default {
});
},

geocodeAddress(address) {
geocodeAddress(address, swLat, swLng, neLat, neLng) {
if (!address) {
return Promise.reject(new Error("Address is required"));
}

if (this.useGoogleOnIos && this.apiKey && Platform.OS === 'ios') {
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
}
// If any of the parameters is not set for the region, we set them all to 0
if (swLat == null || swLng == null || neLat == null || neLng == null){
swLat = 0;
swLng = 0;
neLat = 0;
neLng = 0;
}

return RNGeocoder.geocodeAddress(address, this.language).catch(err => {
return RNGeocoder.geocodeAddress(address, swLat, swLng, neLat, neLng, this.language).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
});
Expand Down