This repository was archived by the owner on Sep 4, 2021. It is now read-only.
-
Notifications
You must be signed in to change notification settings - Fork 180
Expand file tree
/
Copy pathgeocoder.js
More file actions
53 lines (44 loc) · 1.3 KB
/
geocoder.js
File metadata and controls
53 lines (44 loc) · 1.3 KB
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
import { NativeModules } from 'react-native';
import GoogleApi from './googleApi.js';
const { RNGeocoder } = NativeModules;
export default {
apiKey: null,
language: 'en',
onlyGoogle: false,
fallbackToGoogle(key) {
this.apiKey = key;
},
setLanguage(lang) {
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"));
}
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) {
if (!address) {
return Promise.reject(new Error("address is null"));
}
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);
});
}
},
}