From 7e80482c6d6810fc8b54ab581bc4d1aef60cf3fa Mon Sep 17 00:00:00 2001 From: Gregory Date: Thu, 17 Oct 2019 22:13:51 +0300 Subject: [PATCH] allow initialize google places sdk from javascript --- README.md | 9 +++++++++ READMEV2.md | 9 +++++++++ .../rngoogleplaces/RNGooglePlacesModule.java | 19 ++++++++++--------- index.js | 4 ++++ ios/RNGooglePlaces.m | 9 +++++++++ 5 files changed, 41 insertions(+), 9 deletions(-) diff --git a/README.md b/README.md index eb0b880..5c83b3d 100644 --- a/README.md +++ b/README.md @@ -216,6 +216,15 @@ android { import RNGooglePlaces from 'react-native-google-places'; ``` +#### Init library with API key +```javascript +import RNGooglePlaces from 'react-native-google-places'; + +RNGooglePlaces.init("PLACE_YOUR_API_KEY_HERE"); +// don't place you api key directly +// for example use react-native-config +``` + #### Open Autocomplete Modal (e.g as Callback to an onPress event) diff --git a/READMEV2.md b/READMEV2.md index 895d82d..633ac49 100644 --- a/READMEV2.md +++ b/READMEV2.md @@ -199,6 +199,15 @@ If you do **not** have *project-wide properties* defined or want to use a differ import RNGooglePlaces from 'react-native-google-places'; ``` +#### Init library with API key +```javascript +import RNGooglePlaces from 'react-native-google-places'; + +RNGooglePlaces.init("PLACE_YOUR_API_KEY_HERE"); +// don't place you api key directly +// for example use react-native-config +``` + #### Open Autocomplete Modal (e.g as Callback to an onPress event) diff --git a/android/src/main/java/com/arttitude360/reactnative/rngoogleplaces/RNGooglePlacesModule.java b/android/src/main/java/com/arttitude360/reactnative/rngoogleplaces/RNGooglePlacesModule.java index b65868f..efcd5b2 100644 --- a/android/src/main/java/com/arttitude360/reactnative/rngoogleplaces/RNGooglePlacesModule.java +++ b/android/src/main/java/com/arttitude360/reactnative/rngoogleplaces/RNGooglePlacesModule.java @@ -76,15 +76,6 @@ public class RNGooglePlacesModule extends ReactContextBaseJavaModule implements public RNGooglePlacesModule(ReactApplicationContext reactContext) { super(reactContext); - String apiKey = reactContext.getApplicationContext().getString(R.string.places_api_key); - - // Setup Places Client - if (!Places.isInitialized() && !apiKey.equals("")) { - Places.initialize(reactContext.getApplicationContext(), apiKey); - } - - placesClient = Places.createClient(reactContext.getApplicationContext()); - this.reactContext = reactContext; this.reactContext.addActivityEventListener(this); } @@ -121,6 +112,16 @@ public void onActivityResult(Activity activity, final int requestCode, final int * Exposed React's methods */ + @ReactMethod + public void init(String apiKey) { + // Setup Places Client + if (!Places.isInitialized() && !apiKey.equals("")) { + Places.initialize(this.reactContext.getApplicationContext(), apiKey); + } + + placesClient = Places.createClient(this.reactContext.getApplicationContext()); + } + @ReactMethod public void openAutocompleteModal(ReadableMap options, ReadableArray fields, final Promise promise) { diff --git a/index.js b/index.js index 0190504..eac9f91 100644 --- a/index.js +++ b/index.js @@ -28,6 +28,10 @@ class RNGooglePlaces { static placeFieldsDefaults = [] + init(apiKey) { + RNGooglePlacesNative.init(apiKey) + } + openAutocompleteModal(options = {}, placeFields = []) { return RNGooglePlacesNative.openAutocompleteModal({ ...RNGooglePlaces.optionsDefaults, diff --git a/ios/RNGooglePlaces.m b/ios/RNGooglePlaces.m index 4b8f3f9..d351b57 100644 --- a/ios/RNGooglePlaces.m +++ b/ios/RNGooglePlaces.m @@ -10,6 +10,9 @@ #import +@import GooglePlaces; +@import GoogleMaps; + @interface RNGooglePlaces() @property (strong, nonatomic) CLLocationManager *locationManager; @@ -52,6 +55,12 @@ - (void)dealloc self.locationManager = nil; } +RCT_EXPORT_METHOD(init: (NSString *) apiKey) +{ + [GMSPlacesClient provideAPIKey:apiKey]; + [GMSServices provideAPIKey:apiKey]; +} + RCT_EXPORT_METHOD(openAutocompleteModal: (NSDictionary *)options withFields: (NSArray *)fields resolver: (RCTPromiseResolveBlock)resolve