Last active
September 26, 2017 14:07
-
-
Save hanipcode/17f2b77dd16a19ecacd72f1d92e2ecce to your computer and use it in GitHub Desktop.
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
| /* | |
| EDIT!! request-promise mengalami error saat akan digunakan dengan react-native, | |
| maka kita akan menggunakan native fetch dengan bantuan qs (queryString) module. | |
| untuk install gunakan `npm install --save qs` | |
| */ | |
| const qs = require('qs'); | |
| function getPredictionList(searchQuery, key) { | |
| const queryString = qs.stringify({ | |
| input: searchQuery, | |
| type: 'geocode', | |
| key | |
| }); | |
| const uri = `https://maps.googleapis.com/maps/api/place/autocomplete/json?${queryString}`; | |
| return fetch(uri).then(response => response.json()); | |
| } | |
| function getPlaceDetails(placeId, key) { | |
| const queryString = qs.stringify({ | |
| placeid: placeId, | |
| key | |
| }); | |
| const uri = `https://maps.googleapis.com/maps/api/place/details/json?${queryString}`; | |
| return fetch(uri).then(response => response.json()); | |
| } | |
| export function getPredictionWithDetail(searchQuery, key) { | |
| return getPredictionList(searchQuery, key) | |
| .then(result => { | |
| const predictions = result.predictions; | |
| const predictionsPromise = predictions.map(prediction => | |
| getPlaceDetails(prediction.place_id, key) | |
| ); | |
| return Promise.all(predictionsPromise); | |
| }) | |
| .then(data => { | |
| console.log(data); | |
| const predictionDetails = data.map(predictionItem => ({ | |
| name: predictionItem.result.name, | |
| formatted_address: predictionItem.result.formatted_address, | |
| geometry: predictionItem.result.geometry, | |
| placeId: predictionItem.result.place_id | |
| })); | |
| return predictionDetails; | |
| }) | |
| .catch(err => { | |
| console.log(err.toString()); | |
| return err; | |
| }); | |
| } |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment