react-native-geocoder
Version:
react native geocoding and reverse geocoding
35 lines (27 loc) • 857 B
JavaScript
import { NativeModules } from 'react-native';
import GoogleApi from './googleApi.js';
const { RNGeocoder } = NativeModules;
export default {
apiKey: null,
fallbackToGoogle(key) {
this.apiKey = key;
},
geocodePosition(position) {
if (!position || !position.lat || !position.lng) {
return Promise.reject(new Error("invalid position: {lat, lng} required"));
}
return RNGeocoder.geocodePosition(position).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodePosition(this.apiKey, position);
});
},
geocodeAddress(address) {
if (!address) {
return Promise.reject(new Error("address is null"));
}
return RNGeocoder.geocodeAddress(address).catch(err => {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodeAddress(this.apiKey, address);
});
},
}