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

add support language #75

Open
wants to merge 2 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
33 changes: 26 additions & 7 deletions js/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,30 +5,49 @@ const { RNGeocoder } = NativeModules;

export default {
apiKey: null,
language: 'en',
onlyGoogle: false,

fallbackToGoogle(key) {
this.apiKey = key;
},

setLanguage(lang) {
this.language = lang
},

setGoogle(state) {
this.onlyGoogle = state
},

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.setGoogle) {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodePosition(this.apiKey, position);
});
return GoogleApi.geocodePosition(this.apiKey, position, this.language);
} else {
return RNGeocoder.geocodePosition(position).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodePosition(this.apiKey, position, this.language);
});
}
},

geocodeAddress(address) {
if (!address) {
return Promise.reject(new Error("address is null"));
}

return RNGeocoder.geocodeAddress(address).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodeAddress(this.apiKey, address);
});
if (this.setGoogle) {
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
} else {
return RNGeocoder.geocodeAddress(address).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
});
}
},
}
8 changes: 4 additions & 4 deletions js/googleApi.js
Original file line number Diff line number Diff line change
Expand Up @@ -58,20 +58,20 @@ function format(raw) {
}

export default {
geocodePosition(apiKey, position) {
geocodePosition(apiKey, position, language) {
if (!apiKey || !position || !position.lat || !position.lng) {
return Promise.reject(new Error("invalid apiKey / position"));
}

return this.geocodeRequest(`${googleUrl}?key=${apiKey}&latlng=${position.lat},${position.lng}`);
return this.geocodeRequest(`${googleUrl}?key=${apiKey}&latlng=${position.lat},${position.lng}&language=${language}`);
},

geocodeAddress(apiKey, address) {
geocodeAddress(apiKey, address, language) {
if (!apiKey || !address) {
return Promise.reject(new Error("invalid apiKey / address"));
}

return this.geocodeRequest(`${googleUrl}?key=${apiKey}&address=${encodeURI(address)}`);
return this.geocodeRequest(`${googleUrl}?key=${apiKey}&address=${encodeURI(address)}&language=${language}`);
},

async geocodeRequest(url) {
Expand Down
2 changes: 1 addition & 1 deletion package.json
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@
{
"name": "react-native-geocoder",
"version": "0.5.0",
"version": "0.5.1",
"description": "react native geocoding and reverse geocoding",
"main": "index.js",
"scripts": {
Expand Down