-
Notifications
You must be signed in to change notification settings - Fork 5
/
gaode.js
181 lines (145 loc) · 6.32 KB
/
gaode.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
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
module.exports = function (RED) {
var axios = require('axios');
function gaodeNode(config) {
RED.nodes.createNode(this, config);
// Retrieve the config node
this.server = RED.nodes.getNode(config.server);
var node = this;
if (this.server) {
} else {
node.error("没有配置正确的gaode server", msg);
return
}
let gaodeKey = this.server.gaodeKey
node.on('input', function (msg) {
var longitude = config.longitude || msg.payload.longitude
var latitude = config.latitude || msg.payload.latitude
var zoom = config.zoom || msg.payload.zoom || '10'
let locations = `${longitude},${latitude}`
var payload = {}
var image_map = `https://restapi.amap.com/v3/staticmap?location=${locations}&zoom=${zoom}&size=750*500&markers=mid,,A:${locations}&key=${gaodeKey}`
node.log(image_map)
axios({
method: 'get',
url: `http://restapi.amap.com/v3/assistant/coordinate/convert?key=${gaodeKey}&locations=${locations}&coordsys=gps`
}).then(function (response) {
var data = response.data
var status = data['status']
if (status != 1) {
throw new Error(JSON.stringify(data))
}
return axios({
method: 'get',
url: `http://restapi.amap.com/v3/geocode/regeo?key=${gaodeKey}&location=${data.locations}&poitype=&radius=1000&extensions=all&batch=false&roadlevel=0`
})
}).then(function (response) {
var data = response.data
var status = data['status']
if (status != 1) {
throw new Error(JSON.stringify(data))
}
payload.status = 1
payload.location = data['regeocode']['formatted_address']
payload.image = image_map
msg.payload = payload
msg['data'] = data
node.send(msg)
}).catch(function (error) {
payload.status = 0
payload = error.message
msg.payload = payload
msg['data'] = error
node.send(msg)
})
});
}
RED.nodes.registerType("gaode", gaodeNode);
function gaodeDirection(config) {
RED.nodes.createNode(this, config);
// Retrieve the config node
this.server = RED.nodes.getNode(config.server);
var node = this;
if (this.server) {
} else {
node.error("没有配置正确的gaode server", msg);
return
}
let gaodeKey = this.server.gaodeKey
console.log(`key: ${gaodeKey}`)
node.on('input', function (msg) {
var longitude_source = config.longitude_source || msg.payload.longitude_source
var latitude_source = config.latitude_source || msg.payload.latitude_source
let locations_s = `${longitude_source},${latitude_source}`
var longitude_direction = config.longitude_direction || msg.payload.longitude_direction
var latitude_direction = config.latitude_direction || msg.payload.latitude_direction
let locations_d = `${longitude_direction},${latitude_direction}`
var city = config.city || msg.payload.city
var payload = {}
axios({
method: 'get',
url: `http://restapi.amap.com/v3/assistant/coordinate/convert?key=${gaodeKey}&locations=${locations_s}|${locations_d}&coordsys=gps`
}).then(function (response) {
var data = response.data
var status = data['status']
if (status != 1) {
throw new Error(JSON.stringify(data))
}
var locations = data.locations.split(';')
var real_url = getRequestUrl(config, gaodeKey, locations, city)
return axios({
method: 'get',
url: real_url
})
}).then(function (response) {
var data = response.data
var status = data['status']
if (status != 1) {
throw new Error(JSON.stringify(data))
}
var paths = null
if(config.s2 || config.s1) {
paths = data['route']['paths'][0]
}else if(config.s3) {
paths = data['data']['paths'][0]
}else if (config.s4) {
paths = {}
paths['distance'] = data['route']['distance']
paths['duration' ]= data['route']['transits'][0]['duration']
}
payload.status = 1
payload.distance = paths['distance']
payload.duration = paths['duration']
msg.payload = payload
msg['data'] = data
node.send(msg)
}).catch(function (error) {
payload.status = 0
payload = error.message
msg.payload = payload
msg['data'] = error
node.send(msg)
})
});
}
function getRequestUrl(config, gaodeKey, locaions,city) {
if (config.s1) {
return `https://restapi.amap.com/v3/direction/driving?origin=${locaions[0]}&destination=${locaions[1]}&key=${gaodeKey}`
}
if (config.s2) {
return `https://restapi.amap.com/v3/direction/walking?origin=${locaions[0]}&destination=${locaions[1]}&key=${gaodeKey}`
}
if (config.s3) {
return `https://restapi.amap.com/v3/direction/bicycling?origin=${locaions[0]}&destination=${locaions[1]}&key=${gaodeKey}`
}
if (config.s4) {
return `https://restapi.amap.com/v3/direction/transit/integrated?origin=${locaions[0]}&destination=${locaions[1]}&key=${gaodeKey}&city=${city}&cityd=${city}`
}
}
RED.nodes.registerType("gaode-direction", gaodeDirection);
function RemoteServerNode(n) {
RED.nodes.createNode(this, n);
this.name = n.name;
this.gaodeKey = n.gaodeKey;
}
RED.nodes.registerType("gaode-server", RemoteServerNode);
}