Skip to content
New issue

Have a question about this project? Sign up for a free GitHub account to open an issue and contact its maintainers and the community.

By clicking “Sign up for GitHub”, you agree to our terms of service and privacy statement. We’ll occasionally send you account related emails.

Already on GitHub? Sign in to your account

Allow initialize google places SDK from javascript #261

Open
wants to merge 1 commit 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
9 changes: 9 additions & 0 deletions README.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
9 changes: 9 additions & 0 deletions READMEV2.md
Original file line number Diff line number Diff line change
Expand Up @@ -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)


Expand Down
Original file line number Diff line number Diff line change
Expand Up @@ -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);
}
Expand Down Expand Up @@ -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) {

Expand Down
4 changes: 4 additions & 0 deletions index.js
Original file line number Diff line number Diff line change
Expand Up @@ -28,6 +28,10 @@ class RNGooglePlaces {

static placeFieldsDefaults = []

init(apiKey) {
RNGooglePlacesNative.init(apiKey)
}

openAutocompleteModal(options = {}, placeFields = []) {
return RNGooglePlacesNative.openAutocompleteModal({
...RNGooglePlaces.optionsDefaults,
Expand Down
9 changes: 9 additions & 0 deletions ios/RNGooglePlaces.m
Original file line number Diff line number Diff line change
Expand Up @@ -10,6 +10,9 @@

#import <GooglePlaces/GooglePlaces.h>

@import GooglePlaces;
@import GoogleMaps;

@interface RNGooglePlaces() <CLLocationManagerDelegate>

@property (strong, nonatomic) CLLocationManager *locationManager;
Expand Down Expand Up @@ -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
Expand Down