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
61 lines (47 loc) · 1.58 KB
/
Copy pathgeocoder.js
File metadata and controls
61 lines (47 loc) · 1.58 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
54
55
56
57
58
59
60
61
import { NativeModules, Platform } from 'react-native';
import GoogleApi from './googleApi.js';
const { RNGeocoder } = NativeModules;
export default {
apiKey: null,
language: null,
fallbackToGoogle(key) {
this.apiKey = key;
},
setLanguage(language) {
RNGeocoder.setLanguage(language, (result) => {
this.language = result;
});
},
geocodePositionFallback(position) {
if (!this.apiKey) { throw new Error("Google API key required"); }
return GoogleApi.geocodePosition(this.apiKey, position, this.language);
},
geocodeAddressFallback(address) {
if (!this.apiKey) { throw new Error("Google API key required"); }
return GoogleApi.geocodeAddress(this.apiKey, address, this.language);
},
geocodePosition(position) {
if (!position || !position.lat || !position.lng) {
return Promise.reject(new Error("invalid position: {lat, lng} required"));
}
if (this.language && (Platform.OS === 'ios')) {
return this.geocodePositionFallback(position);
}
return RNGeocoder.geocodePosition(position).catch(err => {
if (err.code !== 'NOT_AVAILABLE') { throw err; }
return this.geocodePositionFallback(position);
});
},
geocodeAddress(address) {
if (!address) {
return Promise.reject(new Error("address is null"));
}
if (this.language && (Platform.OS === 'ios')) {
return this.geocodeAddressFallback(address);
}
return RNGeocoder.geocodeAddress(address).catch(err => {
if (err.code !== 'NOT_AVAILABLE') { throw err; }
return this.geocodeAddressFallback(address);
});
},
}