From 386a331fe702209b3e750f72098d655d62a254ce Mon Sep 17 00:00:00 2001 From: Fonov Sergei Date: Mon, 21 May 2018 17:35:21 +0300 Subject: [PATCH 1/2] add support language --- js/geocoder.js | 9 +++++++-- js/googleApi.js | 8 ++++---- 2 files changed, 11 insertions(+), 6 deletions(-) diff --git a/js/geocoder.js b/js/geocoder.js index 3a4563e..8e586c7 100644 --- a/js/geocoder.js +++ b/js/geocoder.js @@ -5,11 +5,16 @@ const { RNGeocoder } = NativeModules; export default { apiKey: null, + language: 'en', fallbackToGoogle(key) { this.apiKey = key; }, + setLanguage(lang) { + this.language = lang + }, + geocodePosition(position) { if (!position || !position.lat || !position.lng) { return Promise.reject(new Error("invalid position: {lat, lng} required")); @@ -17,7 +22,7 @@ export default { return RNGeocoder.geocodePosition(position).catch(err => { if (!this.apiKey) { throw err; } - return GoogleApi.geocodePosition(this.apiKey, position); + return GoogleApi.geocodePosition(this.apiKey, position, this.language); }); }, @@ -28,7 +33,7 @@ export default { return RNGeocoder.geocodeAddress(address).catch(err => { if (!this.apiKey) { throw err; } - return GoogleApi.geocodeAddress(this.apiKey, address); + return GoogleApi.geocodeAddress(this.apiKey, address, this.language); }); }, } diff --git a/js/googleApi.js b/js/googleApi.js index 4358de7..9b73a45 100644 --- a/js/googleApi.js +++ b/js/googleApi.js @@ -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) { From 385c3f817d9509345e20976aa49cc21166eb365a Mon Sep 17 00:00:00 2001 From: Fonov Sergei Date: Mon, 21 May 2018 17:48:14 +0300 Subject: [PATCH 2/2] + --- js/geocoder.js | 24 +++++++++++++++++++----- package.json | 2 +- 2 files changed, 20 insertions(+), 6 deletions(-) diff --git a/js/geocoder.js b/js/geocoder.js index 8e586c7..061eff0 100644 --- a/js/geocoder.js +++ b/js/geocoder.js @@ -6,6 +6,7 @@ const { RNGeocoder } = NativeModules; export default { apiKey: null, language: 'en', + onlyGoogle: false, fallbackToGoogle(key) { this.apiKey = key; @@ -15,15 +16,24 @@ export default { 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, this.language); - }); + } else { + return RNGeocoder.geocodePosition(position).catch(err => { + if (!this.apiKey) { throw err; } + return GoogleApi.geocodePosition(this.apiKey, position, this.language); + }); + } }, geocodeAddress(address) { @@ -31,9 +41,13 @@ export default { return Promise.reject(new Error("address is null")); } - return RNGeocoder.geocodeAddress(address).catch(err => { - if (!this.apiKey) { throw err; } + 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); + }); + } }, } diff --git a/package.json b/package.json index 22e016b..13c927a 100644 --- a/package.json +++ b/package.json @@ -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": {