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

Enable geocoding via google services #48

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
30 changes: 26 additions & 4 deletions README.md
Original file line number Diff line number Diff line change
@@ -1,6 +1,6 @@


# react-native-geocoder
# react-native-geocoder

[![CircleCI](https://circleci.com/gh/devfd/react-native-geocoder/tree/master.svg?style=shield)](https://circleci.com/gh/devfd/react-native-geocoder/tree/master)

Expand Down Expand Up @@ -86,6 +86,30 @@ Geocoder.geocodeAddress('New York').then(res => {
.catch(err => console.log(err))
```

## Force to use google maps geocoding
```
import Geocoder from 'react-native-geocoder';
Geocoder.setApiKey(MY_KEY);
Geocoder.enableGoogleGeocoder()
// Position Geocoding
var NY = {
lat: 40.7809261,
lng: -73.9637594
};

Geocoder.geocodePosition(NY).then(res => {
// res is an Array of geocoding object (see below) from google maps geocoding
})
.catch(err => console.log(err))

// Address Geocoding
Geocoder.geocodeAddress('New York').then(res => {
// res is an Array of geocoding object (see below) from google maps geocoding
})
.catch(err => console.log(err))
```


## Fallback to google maps geocoding

Geocoding services might not be included in some Android devices (Kindle, some 4.1 devices, non-google devices). For those special cases the lib can fallback to the [online google maps geocoding service](https://developers.google.com/maps/documentation/geocoding/intro#Geocoding)
Expand Down Expand Up @@ -128,7 +152,7 @@ both iOS and Android will return the following object:
streetName: String | null,
postalCode: String | null,
locality: String | null, // city name
country: String,
country: String,
countryCode: String
adminArea: String | null
subAdminArea: String | null,
Expand All @@ -143,5 +167,3 @@ iOS does not allow sending multiple geocoding requests simultaneously, hence if

### Android
geocoding may not work on older android devices (4.1) and will not work if Google play services are not available.


26 changes: 22 additions & 4 deletions js/geocoder.js
Original file line number Diff line number Diff line change
Expand Up @@ -5,20 +5,38 @@ const { RNGeocoder } = NativeModules;

export default {
apiKey: null,
useGoogle: false,

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

enableGoogleGeocoder() {
this.useGoogle = true;
},

//I will add new function to keep backward compatibility, but with more appropriate name
setApiKey(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);
});
if (this.useGoogle) {
return this.geocodeWithGoogle(position);
} else {
return RNGeocoder.geocodePosition(position).catch(err => {
return this.geocodeWithGoogle(position);
});
}
},

geocodeWithGoogle(position) {
if (!this.apiKey) { throw err; }
return GoogleApi.geocodePosition(this.apiKey, position);
},

geocodeAddress(address) {
Expand Down