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

Fallback to Google Maps Web API when Geocoder does not work #33

Open
wants to merge 4 commits into
base: master
Choose a base branch
from
Open
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 @@ -39,7 +39,12 @@ public void geocodeAddress(String addressName, Promise promise) {

try {
List<Address> 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) {
Expand Down
20 changes: 16 additions & 4 deletions js/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -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);
});
},

Expand All @@ -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);
});
},
}