diff --git a/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java b/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java index 642ebfb..5549a05 100644 --- a/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java +++ b/android/src/main/java/com/devfd/RNGeocoder/RNGeocoderModule.java @@ -39,7 +39,12 @@ public void geocodeAddress(String addressName, Promise promise) { try { List
addresses = geocoder.getFromLocationName(addressName, 20); - promise.resolve(transform(addresses)); + if(addresses != null && addresses.size() > 0) { + promise.resolve(transform(addresses)); + } else { + promise.reject("NOT_AVAILABLE", "Geocoder returned an empty list"); + } + } catch (IOException e) { diff --git a/js/geocoder.js b/js/geocoder.js index da2c3b7..dc49bfe 100644 --- a/js/geocoder.js +++ b/js/geocoder.js @@ -10,14 +10,26 @@ export default { this.apiKey = key; }, + geocodePositionFallback(position) { + if (!this.apiKey) { throw new Error("Google API key required"); } + + return GoogleApi.geocodePosition(this.apiKey, position); + }, + + geocodeAddressFallback(address) { + if (!this.apiKey) { throw new Error("Google API key required"); } + + return GoogleApi.geocodeAddress(this.apiKey, address); + }, + geocodePosition(position) { if (!position || !position.lat || !position.lng) { return Promise.reject(new Error("invalid position: {lat, lng} required")); } return RNGeocoder.geocodePosition(position).catch(err => { - if (!this.apiKey || err.code !== 'NOT_AVAILABLE') { throw err; } - return GoogleApi.geocodePosition(this.apiKey, position); + if (err.code !== 'NOT_AVAILABLE') { throw err; } + return this.geocodePositionFallback(position); }); }, @@ -27,8 +39,8 @@ export default { } return RNGeocoder.geocodeAddress(address).catch(err => { - if (!this.apiKey || err.code !== 'NOT_AVAILABLE') { throw err; } - return GoogleApi.geocodeAddress(this.apiKey, address); + if (err.code !== 'NOT_AVAILABLE') { throw err; } + return this.geocodeAddressFallback(address); }); }, }