mapapi.js
4.77 KB
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
182
183
184
185
186
187
188
189
190
191
192
193
194
// 引入SDK核心类
const QQMapWX = require('../utils/qqmap-wx-jssdk.min.js');
var app = getApp();
const key = app.globalData.smartCarMapKey;
// 实例化API核心类
const myMap = new QQMapWX({
key: key // 必填
});
/**
* 两点路线规划
*/
function routePlanning(flng, flat, tlng, tlat, succ, mode = 'driving'){
// console.log(flng + "," + flat + "," + tlng + "," + tlat)
var _this = this;
//网络请求设置
// var url = 'https://apis.map.qq.com/ws/direction/v1/driving/?from='+flat+ ',' + flng +'&to='+ tlat + ',' + tlng +'&key=' + key;
var url = `https://apis.map.qq.com/ws/direction/v1/${mode}/?from=${flat},${flng}&to=${tlat},${tlng}&key=${key}`
// console.log(url);
var opt = {
url: url,
method: 'GET',
dataType: 'json',
success: function(res){
console.log(res);
var ret = res.data;
if (succ && typeof succ === 'function') {
if (ret.status != 0) {
// 服务异常
return;
}
succ(ret.result.routes[0]);
}
}
}
wx.request(opt);
}
//
function direction(mode, flng, flat, tlng, tlat, succ) {
// var url = 'https://apis.map.qq.com/uri/v1/routeplan?from='+flat+ ',' + flng +'&to='+ tlat + ',' + tlng +'&key=' + key;
var url = `https://apis.map.qq.com/uri/v1/routeplan?type=${mode}&fromcoord=${flat},${flng}&tocoord=${tlat},${tlng}&key=${key}`
console.log(url)
var opt = {
url: url,
method: 'GET',
dataType: 'json',
success: function (res) {
console.log(res)
var ret = res.data;
if (succ && typeof succ === 'function') {
if (ret.status != 0) {
// 服务异常
return;
}
succ(ret.result.routes[0]);
}
},
}
wx.request(opt)
// myMap.direction({
// mode: mode,
// from:{
// latitude: flat,
// longitude: flng
// },
// to: {
// latitude: tlat,
// longitude: tlng
// },
// success: function (res) {
// if (cb && typeof cb === 'function') {
// console.log(res)
// cb(res);
// }
// },
// fail: function (res) {
// console.log(res);
// }
// })
}
//两点之间的距离
function calculateDistance(flng, flat, tlng, tlat,cb){
// console.log(flng + ","+ flat+","+ tlng+","+ tlat);
//默认计算步行距离
myMap.calculateDistance({
from:{
latitude: flat,
longitude: flng
},
to: [{
latitude: tlat,
longitude: tlng
}],
success: function (res) {
if(res.status == "0"){
if (cb && typeof cb === 'function') {
console.log(res)
cb(res.result.elements[0].distance);
}
}
},
fail: function (res) {
console.log(res);
}
});
}
//
// function reverseAddresss() {
// }
//逆地址解析
function detailedAdrress(tlng, tlat, da, fa, type){
if (tlng && tlat){
var coord_type = 5;
if (null != type) {
coord_type = type
}
myMap.reverseGeocoder({
location: {
latitude: tlat,
longitude: tlng
},
coord_type: coord_type,
success: function (res) {
// console.log(res);
if (res.status == "0") {
if (da && typeof da === 'function') {
da(res.result);
}
}
},
fail: function (res) {
console.log(res);
if (fa && typeof fa === 'function') {
fa();
}
}
});
}
}
/**地址解析 */
const getCoord = (address,callback) =>{
myMap.geocoder({
address:address,
success(res){
callback(res)
}
})
}
function keywordSearch(keyword,loca,iconPath,callback){
// 调用接口
myMap.search({
keyword: keyword, //搜索关键词
location: loca, //设置周边搜索中心点
page_size: 20,
success: function (res) { //搜索成功后的回调
console.log(res);
var mks = []
for (var i = 0; i < res.data.length; i++) {
var obj = res.data[i];
mks.push({ // 获取返回结果,放到mks数组中
_title: obj.title,
id: obj.id,
latitude: obj.location.lat,
longitude: obj.location.lng,
iconPath: iconPath, //图标路径
width: 38,
height: 40,
address: obj.address
})
}
if (res.status == "0") {
if (callback && typeof callback === 'function') {
callback(mks);
}
}
},
fail: function (res) {
console.log(res);
}
});
}
module.exports = {
calculateDistan: calculateDistance,
getDetailedAdrress: detailedAdrress,
routePlanning: routePlanning,
keywordSearch: keywordSearch,
direction,
getCoord
}