From d16f8523cb800c08b515a1529d1b0c8fd35c78cb Mon Sep 17 00:00:00 2001 From: Alex Sinelnikov Date: Tue, 16 May 2017 21:52:02 +0300 Subject: [PATCH 1/2] Enable geocoding via google services --- js/geocoder.js | 21 +++++++++++++++++---- 1 file changed, 17 insertions(+), 4 deletions(-) diff --git a/js/geocoder.js b/js/geocoder.js index 3a4563e..f86ed03 100644 --- a/js/geocoder.js +++ b/js/geocoder.js @@ -5,20 +5,33 @@ const { RNGeocoder } = NativeModules; export default { apiKey: null, + useGoogle: false, fallbackToGoogle(key) { this.apiKey = key; }, + enableGoogleGeocoder() { + this.useGoogle = true; + }, + 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) { throw err; } - return GoogleApi.geocodePosition(this.apiKey, position); - }); + if (this.useGoogle) { + return this.geocodeWithGoogle(position); + } else { + return RNGeocoder.geocodePosition(position).catch(err => { + return this.geocodeWithGoogle(position); + }); + } + }, + + geocodeWithGoogle(position) { + if (!this.apiKey) { throw err; } + return GoogleApi.geocodePosition(this.apiKey, position); }, geocodeAddress(address) { From e0fa4b251ab883005947579bceea7301cfb6d808 Mon Sep 17 00:00:00 2001 From: Alex Sinelnikov Date: Tue, 16 May 2017 22:09:29 +0300 Subject: [PATCH 2/2] Updated readme --- README.md | 30 ++++++++++++++++++++++++++---- js/geocoder.js | 5 +++++ 2 files changed, 31 insertions(+), 4 deletions(-) diff --git a/README.md b/README.md index 8ab2533..84dba7f 100644 --- a/README.md +++ b/README.md @@ -1,6 +1,6 @@ -# react-native-geocoder +# react-native-geocoder [![CircleCI](https://circleci.com/gh/devfd/react-native-geocoder/tree/master.svg?style=shield)](https://circleci.com/gh/devfd/react-native-geocoder/tree/master) @@ -86,6 +86,30 @@ Geocoder.geocodeAddress('New York').then(res => { .catch(err => console.log(err)) ``` +## Force to use google maps geocoding +``` +import Geocoder from 'react-native-geocoder'; +Geocoder.setApiKey(MY_KEY); +Geocoder.enableGoogleGeocoder() +// Position Geocoding +var NY = { + lat: 40.7809261, + lng: -73.9637594 +}; + +Geocoder.geocodePosition(NY).then(res => { + // res is an Array of geocoding object (see below) from google maps geocoding +}) +.catch(err => console.log(err)) + +// Address Geocoding +Geocoder.geocodeAddress('New York').then(res => { + // res is an Array of geocoding object (see below) from google maps geocoding +}) +.catch(err => console.log(err)) +``` + + ## Fallback to google maps geocoding Geocoding services might not be included in some Android devices (Kindle, some 4.1 devices, non-google devices). For those special cases the lib can fallback to the [online google maps geocoding service](https://developers.google.com/maps/documentation/geocoding/intro#Geocoding) @@ -128,7 +152,7 @@ both iOS and Android will return the following object: streetName: String | null, postalCode: String | null, locality: String | null, // city name - country: String, + country: String, countryCode: String adminArea: String | null subAdminArea: String | null, @@ -143,5 +167,3 @@ iOS does not allow sending multiple geocoding requests simultaneously, hence if ### Android geocoding may not work on older android devices (4.1) and will not work if Google play services are not available. - - diff --git a/js/geocoder.js b/js/geocoder.js index f86ed03..db61fc2 100644 --- a/js/geocoder.js +++ b/js/geocoder.js @@ -15,6 +15,11 @@ export default { this.useGoogle = true; }, + //I will add new function to keep backward compatibility, but with more appropriate name + setApiKey(key) { + this.apiKey = key; + }, + geocodePosition(position) { if (!position || !position.lat || !position.lng) { return Promise.reject(new Error("invalid position: {lat, lng} required"));