-
Notifications
You must be signed in to change notification settings - Fork 0
/
maps.js
72 lines (63 loc) · 1.55 KB
/
maps.js
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
62
63
64
65
66
67
68
69
70
71
const makeResponse = (res) => {
console.log(res)
const responseData = {
lat: null,
lng: null,
name: null,
vicinity: null
}
if( res && res.json && res.json.results && res.json.results.length > 0 ){
var firstRes = res.json.results[0]
console.log( 'find result', firstRes )
var resLoc = firstRes.geometry.location
responseData.lat = resLoc.lat
responseData.lng = resLoc.lng
responseData.name = firstRes.name
responseData.vicinity = firstRes.vicinity || firstRes.formatted_address
}
return responseData
}
const requestCallback = (err, res, callback) => {
console.log('received response from google API')
if( err ){
console.log('ERROR ', err)
callback(err)
} else {
const responseData = makeResponse(res)
callback( null, responseData )
}
}
exports.searchNearByLoc = function(key, lat, lng, type, keyword, callback){
var opts = {
keyword: keyword,
type: type,
location: [lat, lng],
rankby: 'distance'
}
var googleMapsClient = require('@google/maps').createClient({
key: key
});
console.log('Request to geocode google maps API', opts)
googleMapsClient.placesNearby(
opts,
function(err, res){
requestCallback(err, res, callback)
}
)
}
exports.searchNearByAddress = function(key, address, type, keyword, callback){
var opts = {
query: keyword + ' near ' + address,
type,
}
var googleMapsClient = require('@google/maps').createClient({
key: key
});
console.log('Request to geocode google maps API', opts)
googleMapsClient.places(
opts,
function(err, res){
requestCallback(err, res, callback)
}
)
}