UNPKG

1.54 kBJavaScriptView Raw
1import _Qs from 'qs';
2var Qs = _Qs && _Qs.default ? _Qs.default : _Qs;
3import * as RED from './service';
4import { parameters } from './endPoints';
5const endpoints = {
6}
7var formatjson = '?format=json';
8var googleservice = RED.createRedService('https://maps.googleapis.com/maps', null, true);
9var queryKey = null;
10var directionsKey = null;
11export const queryOps = { result_type: 'street_address' }
12export default {
13 setKey: (key) => {
14 queryKey = key;
15 },
16 setDirectionsKey: key => {
17 directionsKey = key;
18 },
19 getGeoCodes: async (latitude, longitude, query) => {
20 query = query || queryOps;
21 var qstring = Qs.stringify(Object.assign({}, {
22 latlng: latitude + ',' + longitude,
23 key: queryKey
24 }, query
25 ));
26 return googleservice.get(parameters('api/geocode/json?') + qstring);
27 },
28 getDirections: async (origin, destination, query) => {
29 //https://maps.googleapis.com/maps/api/directions/json?origin=Toronto&destination=Montreal&key=YOUR_API_KEY
30 query = query || {};
31 if (!origin || !destination) {
32 return Promise.reject('no origin provided');
33 }
34 var qstring = Qs.stringify(Object.assign({}, {
35 origin: origin.latitude + ',' + origin.longitude,
36 destination: destination.latitude + ',' + destination.longitude,
37 key: directionsKey || queryKey
38 },
39 query
40 ));
41 return googleservice.get(parameters('api/directions/json?') + qstring);
42 }
43};
\No newline at end of file