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

Enable google geocoder on ios #84

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
10 changes: 5 additions & 5 deletions android/build.gradle
Original file line number Diff line number Diff line change
@@ -1,14 +1,14 @@
apply plugin: 'com.android.library'

android {
compileSdkVersion 25
buildToolsVersion "25.0.2"
compileSdkVersion 28
buildToolsVersion "28.0.3"

defaultConfig {
minSdkVersion 16
targetSdkVersion 25
versionCode 1
versionName "1.0"
targetSdkVersion 27
versionCode 2
versionName "1.1"
ndk {
abiFilters "armeabi-v7a", "x86"
}
Expand Down
5 changes: 5 additions & 0 deletions index.d.ts
Original file line number Diff line number Diff line change
@@ -0,0 +1,5 @@

export interface Geocoder {
fallbackToGoogle: (apiKey: string) => void;
enableGoogleGeocoderIOS: () => void;
}
21 changes: 20 additions & 1 deletion js/geocoder.js
Original file line number Diff line number Diff line change
@@ -1,20 +1,32 @@
import { NativeModules } from 'react-native';
import { NativeModules, Platform } from 'react-native';
import GoogleApi from './googleApi.js';

const { RNGeocoder } = NativeModules;

export default {
apiKey: null,
useGoogleOnIOS: false,

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

enableGoogleGeocoderIOS() {
this.useGoogleOnIOS = Platform.OS === 'ios';
},

geocodePosition(position) {
if (!position || !position.lat || !position.lng) {
return Promise.reject(new Error("invalid position: {lat, lng} required"));
}

if (this.useGoogleOnIOS) {
if (!this.apiKey) {
return Promise.reject(new Error("No api key"));
}
return GoogleApi.geocodePosition(this.apiKey, position);
}

return RNGeocoder.geocodePosition(position).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodePosition(this.apiKey, position);
Expand All @@ -26,6 +38,13 @@ export default {
return Promise.reject(new Error("address is null"));
}

if (this.useGoogleOnIOS) {
if (!this.apiKey) {
return Promise.reject(new Error("No api key"));
}
return GoogleApi.geocodeAddress(this.apiKey, position);
}

return RNGeocoder.geocodeAddress(address).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodeAddress(this.apiKey, address);
Expand Down