@wiajs/core 0.1.15 → 0.1.19
This diff represents the content of publicly available package versions that have been released to one of the supported registries. The information contained in this diff is provided for informational purposes only and reflects changes between package versions as they appear in their respective public registries.
- package/dist/core.common.js +66 -35
- package/dist/core.common.min.js +2 -2
- package/dist/core.esm.js +65 -34
- package/dist/core.esm.min.js +2 -2
- package/dist/core.js +68 -37
- package/dist/core.min.js +2 -2
- package/package.json +2 -3
- package/util/tool.js +21 -14
- package/lib/bmap.js +0 -728
- package/lib/img/compress.js +0 -523
- package/lib/img/util.js +0 -392
package/lib/bmap.js
DELETED
|
@@ -1,728 +0,0 @@
|
|
|
1
|
-
/**
|
|
2
|
-
* 百度地图封装
|
|
3
|
-
* 使用:
|
|
4
|
-
*
|
|
5
|
-
* 1. 百度地图开放平台(`http://lbsyun.baidu.com/`),注册账户。
|
|
6
|
-
* 进入地图开放平台,应用管理里面的我的应用,创建应用。
|
|
7
|
-
* 选择 浏览器端,输入项目名称,创建成功后生成应用 key,比如 eTrip 的 ak:
|
|
8
|
-
* 应用 AK:AsOWTPuxmvBetfxXCT9GRzCo6MLVQyi5
|
|
9
|
-
* 2. 百度地图开放平台设置Referer 白名单,如wia设置为:\*.wia.pub\*
|
|
10
|
-
* 只有白名单里面的域名网页,才能访问。
|
|
11
|
-
* 设置为 \* 表示任何域名网站都能访问,被其他应用访问,次数过多,百度可能收费或封锁
|
|
12
|
-
* 3. import Map from '@wiajs/lib/bmap'
|
|
13
|
-
* 4. _map = new Map(_self, {ak: _ak, el: 'adjust-map'});
|
|
14
|
-
* _selft: 当前Page 实例。
|
|
15
|
-
* ak:第一步申请的应用key
|
|
16
|
-
* el:页面显示地图的 div 层的id,注意,需带上page名称,避免重名冲突
|
|
17
|
-
* 5. _map.show(center = null, geo = true, points = null)
|
|
18
|
-
* point:默认为当前手机位置,注意,获取手机当前位置,必须为https协议。
|
|
19
|
-
* geo:获得当前位置的地理信息,比如建筑之类的,默认为 true
|
|
20
|
-
* points:标注在地图上的点,当前未实现。
|
|
21
|
-
* 6. show事件与周边搜索
|
|
22
|
-
* 如果在地图显示时,需要搜索附近,需监听 show事件,事件处理函数中执行 search
|
|
23
|
-
_map.on('show', async point => {
|
|
24
|
-
let ps = await _map.nearby(
|
|
25
|
-
['公司', '小区', '写字楼', '交叉路口', '银行', '餐厅'],
|
|
26
|
-
point,
|
|
27
|
-
500
|
|
28
|
-
);
|
|
29
|
-
});
|
|
30
|
-
keys:搜索关键字
|
|
31
|
-
point:中心点
|
|
32
|
-
raduis:半径,单位米
|
|
33
|
-
* 7. around 事件
|
|
34
|
-
let geo;
|
|
35
|
-
_map.on('around', v => {
|
|
36
|
-
geo = v;
|
|
37
|
-
_.name('addr').html(v[0].title);
|
|
38
|
-
});
|
|
39
|
-
* 8. 事件数据
|
|
40
|
-
{
|
|
41
|
-
keyword: 关键字,
|
|
42
|
-
dist:距离,单位米
|
|
43
|
-
title: 标题
|
|
44
|
-
address: 地址
|
|
45
|
-
province:省
|
|
46
|
-
city:城市
|
|
47
|
-
district:区
|
|
48
|
-
town:镇
|
|
49
|
-
street:街道
|
|
50
|
-
streetNumber:街道号
|
|
51
|
-
point: {lng, lat},经、维度,百度格式,gps格式已转换为百度格式
|
|
52
|
-
id: 百度uid
|
|
53
|
-
}
|
|
54
|
-
* 9. nearby 搜索周边
|
|
55
|
-
* 7. search 按城市搜索关键字
|
|
56
|
-
* 10. center 调整地图中心点,需确保地图已加载
|
|
57
|
-
*/
|
|
58
|
-
|
|
59
|
-
import {Event} from '@wiajs/core';
|
|
60
|
-
|
|
61
|
-
/* globals
|
|
62
|
-
BMap,
|
|
63
|
-
BMAP_NAVIGATION_CONTROL_SMALL,
|
|
64
|
-
BMAP_STATUS_SUCCESS,
|
|
65
|
-
*/
|
|
66
|
-
|
|
67
|
-
const _scid = '__baidumap';
|
|
68
|
-
|
|
69
|
-
const def = {
|
|
70
|
-
navigation: false,
|
|
71
|
-
around: true,
|
|
72
|
-
};
|
|
73
|
-
|
|
74
|
-
export default class Map extends Event {
|
|
75
|
-
constructor(page, opt) {
|
|
76
|
-
super(opt, [page]);
|
|
77
|
-
|
|
78
|
-
this.opt = {...def, ...opt};
|
|
79
|
-
const {ak, el} = opt;
|
|
80
|
-
this.ak = ak;
|
|
81
|
-
this.el = el;
|
|
82
|
-
this.page = page;
|
|
83
|
-
|
|
84
|
-
// 页面退出时,删除地图引用
|
|
85
|
-
// page.on('hide', () => {
|
|
86
|
-
// const n = $(`#${_scid}`);
|
|
87
|
-
// if (n.length) {
|
|
88
|
-
// let ns = n.nextAll('script');
|
|
89
|
-
// ns = ns.filter((i, v) =>
|
|
90
|
-
// $(v).attr('src').includes('api.map.baidu.com')
|
|
91
|
-
// );
|
|
92
|
-
// ns.remove();
|
|
93
|
-
// n.remove();
|
|
94
|
-
// }
|
|
95
|
-
// });
|
|
96
|
-
this.loadMap();
|
|
97
|
-
}
|
|
98
|
-
|
|
99
|
-
/**
|
|
100
|
-
* 加载百度地图
|
|
101
|
-
*/
|
|
102
|
-
loadMap() {
|
|
103
|
-
if (!this.map) {
|
|
104
|
-
window.__baiduMapReady = () => {
|
|
105
|
-
this.map = new BMap.Map(this.el);
|
|
106
|
-
this.emit('local::ready bmapReady', this.map);
|
|
107
|
-
};
|
|
108
|
-
|
|
109
|
-
if (!$.id(_scid)) {
|
|
110
|
-
const script = document.createElement('script');
|
|
111
|
-
script.id = _scid;
|
|
112
|
-
script.src = `http://api.map.baidu.com/api?v=3.0&ak=${this.ak}&callback=__baiduMapReady`;
|
|
113
|
-
document.body.appendChild(script);
|
|
114
|
-
// debugger;
|
|
115
|
-
// this.page.view.append(script);
|
|
116
|
-
} else {
|
|
117
|
-
this.map = new BMap.Map(this.el);
|
|
118
|
-
this.emit('local::ready bmapReady', this.map);
|
|
119
|
-
}
|
|
120
|
-
}
|
|
121
|
-
}
|
|
122
|
-
|
|
123
|
-
/**
|
|
124
|
-
* BMap已经加载,将body的脚本引用删除,避免干扰其他层
|
|
125
|
-
*/
|
|
126
|
-
removeSc() {
|
|
127
|
-
const n = $(`#${_scid}`);
|
|
128
|
-
if (n.length) {
|
|
129
|
-
let ns = n.nextAll('script');
|
|
130
|
-
ns = ns.filter((i, v) => $(v).attr('src').includes('api.map.baidu.com'));
|
|
131
|
-
ns.remove();
|
|
132
|
-
n.remove();
|
|
133
|
-
}
|
|
134
|
-
}
|
|
135
|
-
|
|
136
|
-
getMap() {
|
|
137
|
-
return new Promise((res, rej) => {
|
|
138
|
-
if (this.map) res(this.map);
|
|
139
|
-
else {
|
|
140
|
-
let i = 0;
|
|
141
|
-
const tm = setInterval(async () => {
|
|
142
|
-
i++;
|
|
143
|
-
console.log('setInterval', {i});
|
|
144
|
-
if (i >= 100) rej(new Error('加载百度地图失败!'));
|
|
145
|
-
else if (this.map) {
|
|
146
|
-
clearInterval(tm);
|
|
147
|
-
res(this.map);
|
|
148
|
-
$.nextTick(this.removeSc);
|
|
149
|
-
}
|
|
150
|
-
}, 40);
|
|
151
|
-
}
|
|
152
|
-
});
|
|
153
|
-
}
|
|
154
|
-
|
|
155
|
-
/**
|
|
156
|
-
* 显示地图
|
|
157
|
-
* @param {object} center 中心点,数组[经, 纬] or {lng: 经, lat: 维}
|
|
158
|
-
* @param {boolean} around 查询周边
|
|
159
|
-
* @param {array} points 其他坐标
|
|
160
|
-
*/
|
|
161
|
-
async show(center = null, around = true, points = null) {
|
|
162
|
-
const map = await this.getMap();
|
|
163
|
-
this.showMap(center, around, points);
|
|
164
|
-
}
|
|
165
|
-
|
|
166
|
-
/**
|
|
167
|
-
* 使用微信接口进行定位
|
|
168
|
-
*
|
|
169
|
-
* @param {string} type 坐标类型
|
|
170
|
-
* @param {Function} success 成功执行
|
|
171
|
-
* @param {Function} fail 失败执行
|
|
172
|
-
* @param {Function} complete 完成后执行
|
|
173
|
-
*/
|
|
174
|
-
getLocWx(type, success, fail, complete) {
|
|
175
|
-
(type = type || 'gcj02'), (success = success || function () {});
|
|
176
|
-
fail = fail || function () {};
|
|
177
|
-
complete = complete || function () {};
|
|
178
|
-
wx.getLocation({
|
|
179
|
-
type: type,
|
|
180
|
-
success: success,
|
|
181
|
-
fail: fail,
|
|
182
|
-
complete: complete,
|
|
183
|
-
});
|
|
184
|
-
}
|
|
185
|
-
|
|
186
|
-
/**
|
|
187
|
-
* 获取当前位置经纬度数组,百度坐标格式
|
|
188
|
-
*/
|
|
189
|
-
async getLoc() {
|
|
190
|
-
try {
|
|
191
|
-
if (!navigator.geolocation) {
|
|
192
|
-
alert(
|
|
193
|
-
'该手机浏览器不支持地理定位,请升级浏览器或设置里面打开浏览器定位权限!'
|
|
194
|
-
);
|
|
195
|
-
return;
|
|
196
|
-
}
|
|
197
|
-
|
|
198
|
-
// let pm = new Promise((res, rej) => {
|
|
199
|
-
// navigator.geolocation.getCurrentPosition(res, rej, {
|
|
200
|
-
// enableHighAcuracy: true, // 指示浏览器获取高精度的位置,默认为false
|
|
201
|
-
// timeout: 5000, // 超时时间,默认不限时,单位为毫秒
|
|
202
|
-
// maximumAge: 10000, // 有效期,此参数指定缓存多久
|
|
203
|
-
// });
|
|
204
|
-
// });
|
|
205
|
-
// const pos = await pm().catch(e => {
|
|
206
|
-
// let msg = '';
|
|
207
|
-
// switch (e.code) {
|
|
208
|
-
// case e.PERMISSION_DENIED:
|
|
209
|
-
// msg = '用户不允许地理定位';
|
|
210
|
-
// break;
|
|
211
|
-
// case e.POSITION_UNAVAILABLE:
|
|
212
|
-
// msg = '无法获取当前位置';
|
|
213
|
-
// break;
|
|
214
|
-
// case e.TIMEOUT:
|
|
215
|
-
// msg = '操作超时';
|
|
216
|
-
// break;
|
|
217
|
-
// case er.UNKNOWN_ERROR:
|
|
218
|
-
// msg = '未知错误';
|
|
219
|
-
// break;
|
|
220
|
-
// }
|
|
221
|
-
// throw new Error(msg);
|
|
222
|
-
// });
|
|
223
|
-
// const {longitude: lon, latitude: lat} = pos.coords;
|
|
224
|
-
const lng = 106.495419; // 106.574;
|
|
225
|
-
const lat = 29.617266; // 29.5759;
|
|
226
|
-
console.log('getLoc gps', {lng, lat});
|
|
227
|
-
|
|
228
|
-
const map = await this.getMap();
|
|
229
|
-
// 将HTML5定位获取的经纬度,转换成适应于百度定位的经纬度
|
|
230
|
-
let p = new BMap.Point(lng, lat);
|
|
231
|
-
const convertor = new BMap.Convertor();
|
|
232
|
-
return new Promise((res, rej) => {
|
|
233
|
-
convertor.translate([p], 1, 5, (data, status, msg) => {
|
|
234
|
-
if (data.status)
|
|
235
|
-
rej(new Error(`转换坐标出错 status:${status} msg:${msg}`));
|
|
236
|
-
else {
|
|
237
|
-
[p] = data.points;
|
|
238
|
-
console.log('getLoc baidu', p);
|
|
239
|
-
p = [p.lng, p.lat];
|
|
240
|
-
this.emit('local::loc bmapLoc', p);
|
|
241
|
-
res(p);
|
|
242
|
-
}
|
|
243
|
-
});
|
|
244
|
-
});
|
|
245
|
-
} catch (e) {
|
|
246
|
-
console.error('getLoc exp.', {msg: e.message});
|
|
247
|
-
alert(e.message);
|
|
248
|
-
}
|
|
249
|
-
}
|
|
250
|
-
|
|
251
|
-
/**
|
|
252
|
-
* 获得两点距离,失败返回 -1
|
|
253
|
-
*/
|
|
254
|
-
async dist(p1, p2) {
|
|
255
|
-
let R = -1;
|
|
256
|
-
if (!p2) p2 = await this.getLoc();
|
|
257
|
-
if (!p1 || !p2) return R;
|
|
258
|
-
|
|
259
|
-
p1 = bpoint(p1);
|
|
260
|
-
p2 = bpoint(p2);
|
|
261
|
-
const map = await this.getMap();
|
|
262
|
-
R = Math.round(map.getDistance(p1, p2));
|
|
263
|
-
|
|
264
|
-
return R;
|
|
265
|
-
}
|
|
266
|
-
|
|
267
|
-
/**
|
|
268
|
-
* 显示地图
|
|
269
|
-
* @param {object} center 中心点,数组[经, 纬] or {lng: 经, lat: 维}
|
|
270
|
-
* @param {Boolean} around 周边
|
|
271
|
-
* @param {Array} points 其他坐标
|
|
272
|
-
*/
|
|
273
|
-
async showMap(center = null, around = true, points = null) {
|
|
274
|
-
// 转为为 Point 对象
|
|
275
|
-
let point = center;
|
|
276
|
-
if (!center) point = await this.getLoc();
|
|
277
|
-
|
|
278
|
-
point = bpoint(point);
|
|
279
|
-
|
|
280
|
-
// 初始化地图
|
|
281
|
-
// 添加平移缩放控件
|
|
282
|
-
if (this.opt.navigation) {
|
|
283
|
-
this.map.addControl(
|
|
284
|
-
// BMAP_NAVIGATION_CONTROL_LARGE 表示显示完整的平移缩放控件。
|
|
285
|
-
// BMAP_NAVIGATION_CONTROL_SMALL 表示显示小型的平移缩放控件。
|
|
286
|
-
// BMAP_NAVIGATION_CONTROL_PAN 表示只显示控件的平移部分功能。
|
|
287
|
-
// BMAP_NAVIGATION_CONTROL_ZOOM 表示只显示控件的缩放部分功能。
|
|
288
|
-
|
|
289
|
-
new BMap.NavigationControl({
|
|
290
|
-
type: BMAP_NAVIGATION_CONTROL_SMALL,
|
|
291
|
-
enableGeolocation: false, // 在浏览器或者安卓手机下精准定位
|
|
292
|
-
})
|
|
293
|
-
);
|
|
294
|
-
}
|
|
295
|
-
|
|
296
|
-
// 前提是使用https协议的网站
|
|
297
|
-
// 定位到当前地址
|
|
298
|
-
// var geolocation = new BMap.Geolocation();
|
|
299
|
-
// geolocation.getCurrentPosition(
|
|
300
|
-
// function (r) {
|
|
301
|
-
// showLocationInMap(this.getStatus(), r);
|
|
302
|
-
// },
|
|
303
|
-
// {enableHighAccuracy: true}
|
|
304
|
-
// );
|
|
305
|
-
|
|
306
|
-
// this.map.addControl(new BMap.ScaleControl()); // 添加比例尺控件
|
|
307
|
-
// this.map.addControl(new BMap.OverviewMapControl()); // 添加缩略地图控件
|
|
308
|
-
// 仅当设置城市信息时,MapTypeControl的切换功能可用
|
|
309
|
-
// map.addControl(new BMap.MapTypeControl()); // 地图类型控件,默认位于地图右上方。
|
|
310
|
-
// map.setCurrentCity("重庆");
|
|
311
|
-
|
|
312
|
-
this.map.clearOverlays();
|
|
313
|
-
this.map.addOverlay(new BMap.Marker(point));
|
|
314
|
-
// var label = new BMap.Label('转换后的百度坐标(正确)', {
|
|
315
|
-
// offset: new BMap.Size(20, -10),
|
|
316
|
-
// });
|
|
317
|
-
// marker.setLabel(label); //添加 label
|
|
318
|
-
console.log('showMap center:%o', point);
|
|
319
|
-
// 放大级别:16 级,默认 18 级
|
|
320
|
-
// 防止地图偏移
|
|
321
|
-
$.nextTick(() => this.map.centerAndZoom(point, 16));
|
|
322
|
-
this.emit('local::show bmapShow', [point.lng, point.lat]);
|
|
323
|
-
if (around) this.around(point);
|
|
324
|
-
}
|
|
325
|
-
|
|
326
|
-
/**
|
|
327
|
-
* 调整地图中心,调用前,需确保地图已加载
|
|
328
|
-
* @param {object} center 中心点,数组[经, 纬] or {lng: 经, lat: 维}
|
|
329
|
-
*/
|
|
330
|
-
async center(center = null) {
|
|
331
|
-
// 转为为 Point 对象
|
|
332
|
-
if (!center) return;
|
|
333
|
-
|
|
334
|
-
const point = bpoint(center);
|
|
335
|
-
|
|
336
|
-
// 初始化地图
|
|
337
|
-
// 添加平移缩放控件
|
|
338
|
-
if (this.opt.navigation) {
|
|
339
|
-
this.map.addControl(
|
|
340
|
-
// BMAP_NAVIGATION_CONTROL_LARGE 表示显示完整的平移缩放控件。
|
|
341
|
-
// BMAP_NAVIGATION_CONTROL_SMALL 表示显示小型的平移缩放控件。
|
|
342
|
-
// BMAP_NAVIGATION_CONTROL_PAN 表示只显示控件的平移部分功能。
|
|
343
|
-
// BMAP_NAVIGATION_CONTROL_ZOOM 表示只显示控件的缩放部分功能。
|
|
344
|
-
|
|
345
|
-
new BMap.NavigationControl({
|
|
346
|
-
type: BMAP_NAVIGATION_CONTROL_SMALL,
|
|
347
|
-
enableGeolocation: false, // 在浏览器或者安卓手机下精准定位
|
|
348
|
-
})
|
|
349
|
-
);
|
|
350
|
-
}
|
|
351
|
-
|
|
352
|
-
this.map.clearOverlays();
|
|
353
|
-
this.map.addOverlay(new BMap.Marker(point));
|
|
354
|
-
// 放大级别:16 级,默认 18 级
|
|
355
|
-
$.nextTick(() => this.map.centerAndZoom(point, 16));
|
|
356
|
-
this.emit('local::center bmapCenter', [point.lng, point.lat]);
|
|
357
|
-
}
|
|
358
|
-
|
|
359
|
-
/**
|
|
360
|
-
* 周边
|
|
361
|
-
* @param {*} center
|
|
362
|
-
*/
|
|
363
|
-
around(center) {
|
|
364
|
-
const point = bpoint(center);
|
|
365
|
-
const gc = new BMap.Geocoder();
|
|
366
|
-
gc.getLocation(point, r => {
|
|
367
|
-
console.log('around', {r});
|
|
368
|
-
|
|
369
|
-
const title = r.address;
|
|
370
|
-
const {address} = r;
|
|
371
|
-
|
|
372
|
-
const {
|
|
373
|
-
province,
|
|
374
|
-
city,
|
|
375
|
-
district,
|
|
376
|
-
town,
|
|
377
|
-
street,
|
|
378
|
-
streetNumber,
|
|
379
|
-
} = r.addressComponents;
|
|
380
|
-
|
|
381
|
-
// 周边
|
|
382
|
-
let ps = [];
|
|
383
|
-
const surs = [];
|
|
384
|
-
r.surroundingPois.forEach(p => {
|
|
385
|
-
// const sur = surs.find(v => v.Ki === '房地产');
|
|
386
|
-
// if (!sur) sur = srus[0];
|
|
387
|
-
// if (sur)
|
|
388
|
-
// addr = `${city}${district}${town}${street}${streetNumber}${sur.title}`;
|
|
389
|
-
// title = `${sur.title} | ${sur.address}`;
|
|
390
|
-
// 距离
|
|
391
|
-
const dist = Math.round(
|
|
392
|
-
this.map.getDistance(point, new BMap.Point(p.point.lng, p.point.lat))
|
|
393
|
-
);
|
|
394
|
-
|
|
395
|
-
surs.push({
|
|
396
|
-
keyword: p.Ki,
|
|
397
|
-
dist,
|
|
398
|
-
title: p.title,
|
|
399
|
-
address: p.address,
|
|
400
|
-
province,
|
|
401
|
-
city,
|
|
402
|
-
district,
|
|
403
|
-
town,
|
|
404
|
-
street,
|
|
405
|
-
streetNumber,
|
|
406
|
-
point: [p.point.lng, p.point.lat],
|
|
407
|
-
id: p.uid,
|
|
408
|
-
});
|
|
409
|
-
});
|
|
410
|
-
|
|
411
|
-
if (surs.length) {
|
|
412
|
-
ps = surs.sort((a, b) => a.dist - b.dist);
|
|
413
|
-
}
|
|
414
|
-
|
|
415
|
-
// 没有周边
|
|
416
|
-
if (!ps.length) {
|
|
417
|
-
// 距离
|
|
418
|
-
const dist = Math.round(
|
|
419
|
-
this.map.getDistance(point, new BMap.Point(r.point.lng, r.point.lat))
|
|
420
|
-
);
|
|
421
|
-
|
|
422
|
-
ps.push({
|
|
423
|
-
keyword: '缺省',
|
|
424
|
-
dist,
|
|
425
|
-
title,
|
|
426
|
-
address,
|
|
427
|
-
province,
|
|
428
|
-
city,
|
|
429
|
-
district,
|
|
430
|
-
town,
|
|
431
|
-
street,
|
|
432
|
-
streetNumber,
|
|
433
|
-
point: [r.point.lng, r.point.lat],
|
|
434
|
-
id: 0,
|
|
435
|
-
});
|
|
436
|
-
}
|
|
437
|
-
|
|
438
|
-
console.log('around', {ps}); // 地址信息
|
|
439
|
-
this.emit('local::around bmapAround', ps);
|
|
440
|
-
});
|
|
441
|
-
}
|
|
442
|
-
|
|
443
|
-
/**
|
|
444
|
-
* 将地址解析结果显示在地图上,并调整地图视野
|
|
445
|
-
* @param {*} word 地址
|
|
446
|
-
* @param {*} city 城市
|
|
447
|
-
*/
|
|
448
|
-
getPoint(word, city) {
|
|
449
|
-
const gc = new BMap.Geocoder();
|
|
450
|
-
gc.getPoint(
|
|
451
|
-
word,
|
|
452
|
-
point => {
|
|
453
|
-
if (point) {
|
|
454
|
-
this.map.centerAndZoom(point, 16);
|
|
455
|
-
this.map.addOverlay(new BMap.Marker(point));
|
|
456
|
-
}
|
|
457
|
-
},
|
|
458
|
-
city
|
|
459
|
-
);
|
|
460
|
-
}
|
|
461
|
-
|
|
462
|
-
onsearch(local, point, rs, res) {
|
|
463
|
-
// 判断状态是否正确
|
|
464
|
-
if (local.getStatus() === BMAP_STATUS_SUCCESS) {
|
|
465
|
-
console.log('onsearch', {rs});
|
|
466
|
-
const ps = [];
|
|
467
|
-
rs.forEach(r => {
|
|
468
|
-
// 每个关键字返回多页,第一次返回第一页内容
|
|
469
|
-
// const pgcnt = r.getNumPages();
|
|
470
|
-
// console.log('onsearch', {pgcnt});
|
|
471
|
-
const {keyword} = r;
|
|
472
|
-
|
|
473
|
-
for (let i = 0; i < r.getCurrentNumPois(); i++) {
|
|
474
|
-
const q = r.getPoi(i);
|
|
475
|
-
console.log('onsearch getPoi', {q});
|
|
476
|
-
const {title, province, city, address, uid: id} = q;
|
|
477
|
-
// 距离
|
|
478
|
-
let dist = 0;
|
|
479
|
-
if (point) dist = this.dist(point, q.point);
|
|
480
|
-
|
|
481
|
-
ps.push({
|
|
482
|
-
keyword,
|
|
483
|
-
dist,
|
|
484
|
-
title,
|
|
485
|
-
province,
|
|
486
|
-
city,
|
|
487
|
-
address,
|
|
488
|
-
point: [q.point.lng, q.point.lat],
|
|
489
|
-
id,
|
|
490
|
-
});
|
|
491
|
-
}
|
|
492
|
-
});
|
|
493
|
-
if (point) ps.sort((a, b) => a.dist - b.dist);
|
|
494
|
-
console.log('onsearch', {ps});
|
|
495
|
-
|
|
496
|
-
res(ps);
|
|
497
|
-
|
|
498
|
-
// var s = [];
|
|
499
|
-
// for (var i = 0; i < rs.getCurrentNumPois(); i++) {
|
|
500
|
-
// s.push(rs.getPoi(i).title + ', ' + rs.getPoi(i).address);
|
|
501
|
-
// }
|
|
502
|
-
// document.getElementById('log').innerHTML = s.join('<br>');
|
|
503
|
-
}
|
|
504
|
-
}
|
|
505
|
-
|
|
506
|
-
/**
|
|
507
|
-
* 圆形周边搜索
|
|
508
|
-
* @param {*} word 关键字
|
|
509
|
-
* @param {*} center 中心点
|
|
510
|
-
* @param {*} radius 半径,单位 米
|
|
511
|
-
* @param {*} count 每个关键词,返回结果数量,缺省 10个
|
|
512
|
-
*/
|
|
513
|
-
nearby(word, center, radius, count = 10) {
|
|
514
|
-
const point = bpoint(center);
|
|
515
|
-
return new Promise((res, rej) => {
|
|
516
|
-
const local = new BMap.LocalSearch(this.map, {
|
|
517
|
-
// renderOptions: {map: map, autoViewport: true},
|
|
518
|
-
pageCapacity: count,
|
|
519
|
-
onSearchComplete: rs => this.onsearch(local, point, rs, res),
|
|
520
|
-
});
|
|
521
|
-
local.searchNearby(word, point, radius);
|
|
522
|
-
// const rs = local.getResults();
|
|
523
|
-
});
|
|
524
|
-
}
|
|
525
|
-
|
|
526
|
-
/**
|
|
527
|
-
* 圆形周边搜索
|
|
528
|
-
* @param {*} word 关键字
|
|
529
|
-
* @param {*} center 中心点
|
|
530
|
-
* @param {*} radius 半径,单位 米
|
|
531
|
-
* @param {*} count 每个关键词,返回结果数量,缺省 30个
|
|
532
|
-
*/
|
|
533
|
-
search(word, center, count = 30) {
|
|
534
|
-
const point = bpoint(center);
|
|
535
|
-
return new Promise((res, rej) => {
|
|
536
|
-
const local = new BMap.LocalSearch(this.map, {
|
|
537
|
-
// renderOptions: {map: map, autoViewport: true},
|
|
538
|
-
pageCapacity: count,
|
|
539
|
-
onSearchComplete: rs => this.onsearch(local, point, rs, res),
|
|
540
|
-
});
|
|
541
|
-
local.search(word);
|
|
542
|
-
// const rs = local.getResults();
|
|
543
|
-
});
|
|
544
|
-
}
|
|
545
|
-
|
|
546
|
-
/**
|
|
547
|
-
* POI周边检索
|
|
548
|
-
*
|
|
549
|
-
* @param {Object} param 检索配置
|
|
550
|
-
* 参数对象结构可以参考
|
|
551
|
-
* http://lbsyun.baidu.com/index.php?title=webapi/guide/webservice-placeapi
|
|
552
|
-
*/
|
|
553
|
-
/* search(param) {
|
|
554
|
-
var that = this;
|
|
555
|
-
param = param || {};
|
|
556
|
-
let searchparam = {
|
|
557
|
-
query: param['query'] || '生活服务$美食&酒店',
|
|
558
|
-
scope: param['scope'] || 1,
|
|
559
|
-
filter: param['filter'] || '',
|
|
560
|
-
coord_type: param['coord_type'] || 2,
|
|
561
|
-
page_size: param['page_size'] || 10,
|
|
562
|
-
page_num: param['page_num'] || 0,
|
|
563
|
-
output: param['output'] || 'json',
|
|
564
|
-
ak: that.ak,
|
|
565
|
-
sn: param['sn'] || '',
|
|
566
|
-
timestamp: param['timestamp'] || '',
|
|
567
|
-
radius: param['radius'] || 2000,
|
|
568
|
-
ret_coordtype: 'gcj02ll',
|
|
569
|
-
};
|
|
570
|
-
let otherparam = {
|
|
571
|
-
iconPath: param['iconPath'],
|
|
572
|
-
iconTapPath: param['iconTapPath'],
|
|
573
|
-
width: param['width'],
|
|
574
|
-
height: param['height'],
|
|
575
|
-
alpha: param['alpha'] || 1,
|
|
576
|
-
success: param['success'] || function () {},
|
|
577
|
-
fail: param['fail'] || function () {},
|
|
578
|
-
};
|
|
579
|
-
let type = 'gcj02';
|
|
580
|
-
let locationsuccess = function (result) {
|
|
581
|
-
searchparam['location'] = result['latitude'] + ',' + result['longitude'];
|
|
582
|
-
wx.request({
|
|
583
|
-
url: 'https://api.map.baidu.com/place/v2/search',
|
|
584
|
-
data: searchparam,
|
|
585
|
-
header: {
|
|
586
|
-
'content-type': 'application/json',
|
|
587
|
-
},
|
|
588
|
-
method: 'GET',
|
|
589
|
-
success(data) {
|
|
590
|
-
let res = data['data'];
|
|
591
|
-
if (res['status'] === 0) {
|
|
592
|
-
let poiArr = res['results'];
|
|
593
|
-
// outputRes 包含两个对象,
|
|
594
|
-
// originalData为百度接口返回的原始数据
|
|
595
|
-
// wxMarkerData为小程序规范的marker格式
|
|
596
|
-
let outputRes = {};
|
|
597
|
-
outputRes['originalData'] = res;
|
|
598
|
-
outputRes['wxMarkerData'] = [];
|
|
599
|
-
for (let i = 0; i < poiArr.length; i++) {
|
|
600
|
-
outputRes['wxMarkerData'][i] = {
|
|
601
|
-
id: i,
|
|
602
|
-
latitude: poiArr[i]['location']['lat'],
|
|
603
|
-
longitude: poiArr[i]['location']['lng'],
|
|
604
|
-
title: poiArr[i]['name'],
|
|
605
|
-
iconPath: otherparam['iconPath'],
|
|
606
|
-
iconTapPath: otherparam['iconTapPath'],
|
|
607
|
-
address: poiArr[i]['address'],
|
|
608
|
-
telephone: poiArr[i]['telephone'],
|
|
609
|
-
alpha: otherparam['alpha'],
|
|
610
|
-
width: otherparam['width'],
|
|
611
|
-
height: otherparam['height'],
|
|
612
|
-
};
|
|
613
|
-
}
|
|
614
|
-
otherparam.success(outputRes);
|
|
615
|
-
} else {
|
|
616
|
-
otherparam.fail({
|
|
617
|
-
errMsg: res['message'],
|
|
618
|
-
statusCode: res['status'],
|
|
619
|
-
});
|
|
620
|
-
}
|
|
621
|
-
},
|
|
622
|
-
fail(data) {
|
|
623
|
-
otherparam.fail(data);
|
|
624
|
-
},
|
|
625
|
-
});
|
|
626
|
-
};
|
|
627
|
-
let locationfail = function (result) {
|
|
628
|
-
otherparam.fail(result);
|
|
629
|
-
};
|
|
630
|
-
let locationcomplete = function (result) {};
|
|
631
|
-
if (!param['location']) {
|
|
632
|
-
that.getWXLocation(type, locationsuccess, locationfail, locationcomplete);
|
|
633
|
-
} else {
|
|
634
|
-
let longitude = param.location.split(',')[1];
|
|
635
|
-
let latitude = param.location.split(',')[0];
|
|
636
|
-
let errMsg = 'input location';
|
|
637
|
-
let res = {
|
|
638
|
-
errMsg: errMsg,
|
|
639
|
-
latitude: latitude,
|
|
640
|
-
longitude: longitude,
|
|
641
|
-
};
|
|
642
|
-
locationsuccess(res);
|
|
643
|
-
}
|
|
644
|
-
}
|
|
645
|
-
*/
|
|
646
|
-
}
|
|
647
|
-
|
|
648
|
-
/**
|
|
649
|
-
* 转换为百度Point 对象
|
|
650
|
-
* @param {object} point 坐标
|
|
651
|
-
*/
|
|
652
|
-
function bpoint(point) {
|
|
653
|
-
if (!point) return null;
|
|
654
|
-
|
|
655
|
-
let R = point;
|
|
656
|
-
if (!(point instanceof BMap.Point)) {
|
|
657
|
-
if ($.isArray(point)) R = new BMap.Point(point[0], point[1]);
|
|
658
|
-
else R = new BMap.Point(point.lng, point.lat);
|
|
659
|
-
}
|
|
660
|
-
|
|
661
|
-
return R;
|
|
662
|
-
}
|
|
663
|
-
|
|
664
|
-
// 两点之间的距离
|
|
665
|
-
|
|
666
|
-
// var pointA = new BMap.Point(data.points[0].lng,data.points[0].lat);//用户的经纬度
|
|
667
|
-
// var pointB = new BMap.Point(106.557308,29.615562);//从数据库中取出商家的经纬度
|
|
668
|
-
// alert('您到商家的距离是:'+(map.getDistance(pointA,pointB)).toFixed(2)+' 米。'); //获取两点距离,保留小数点后两位
|
|
669
|
-
|
|
670
|
-
// 地图展示当前位置
|
|
671
|
-
/* var accuracy = '';
|
|
672
|
-
var longitude = '';
|
|
673
|
-
var latitude = '';
|
|
674
|
-
function showLocationInMap(status, r) {
|
|
675
|
-
if (status == BMAP_STATUS_SUCCESS) {
|
|
676
|
-
//检索成功。对应数值“0”。
|
|
677
|
-
var mk = new BMap.Marker(r.point);
|
|
678
|
-
map.addOverlay(mk);
|
|
679
|
-
map.panTo(r.point);
|
|
680
|
-
|
|
681
|
-
//console.log(r.accuracy);
|
|
682
|
-
//console.log(r.point.lng);
|
|
683
|
-
//console.log(r.point.lat);
|
|
684
|
-
accuracy = r.accuracy;
|
|
685
|
-
longitude = r.point.lng;
|
|
686
|
-
latitude = r.point.lat;
|
|
687
|
-
|
|
688
|
-
//用所定位的经纬度查找所在地省市街道等信息
|
|
689
|
-
var point = new BMap.Point(r.point.lng, r.point.lat);
|
|
690
|
-
var gc = new BMap.Geocoder();
|
|
691
|
-
gc.getLocation(point, function (rs) {
|
|
692
|
-
var addComp = rs.addressComponents;
|
|
693
|
-
//console.log(rs.address);//地址信息
|
|
694
|
-
actual_address = rs.address;
|
|
695
|
-
var address = new BMap.Label(rs.address, {
|
|
696
|
-
offset: new BMap.Size(-80, -25),
|
|
697
|
-
});
|
|
698
|
-
mk.setLabel(address); //添加地址标注
|
|
699
|
-
});
|
|
700
|
-
} else if (status == BMAP_STATUS_CITY_LIST) {
|
|
701
|
-
//城市列表。对应数值“1”。
|
|
702
|
-
common.alertMsg('城市列表', 0);
|
|
703
|
-
} else if (status == BMAP_STATUS_UNKNOWN_LOCATION) {
|
|
704
|
-
//位置结果未知。对应数值“2”。
|
|
705
|
-
common.alertMsg('位置结果未知', 0);
|
|
706
|
-
} else if (status == BMAP_STATUS_UNKNOWN_ROUTE) {
|
|
707
|
-
//导航结果未知。对应数值“3”。
|
|
708
|
-
common.alertMsg('导航结果未知', 0);
|
|
709
|
-
} else if (status == BMAP_STATUS_INVALID_KEY) {
|
|
710
|
-
//非法密钥。对应数值“4”。
|
|
711
|
-
common.alertMsg('非法密钥', 0);
|
|
712
|
-
} else if (status == BMAP_STATUS_INVALID_REQUEST) {
|
|
713
|
-
//非法请求。对应数值“5”。
|
|
714
|
-
common.alertMsg('非法请求', 0);
|
|
715
|
-
} else if (status == BMAP_STATUS_PERMISSION_DENIED) {
|
|
716
|
-
//没有权限。对应数值“6”。(自 1.1 新增)
|
|
717
|
-
common.alertMsg('没有权限', 0);
|
|
718
|
-
} else if (status == BMAP_STATUS_SERVICE_UNAVAILABLE) {
|
|
719
|
-
//服务不可用。对应数值“7”。(自 1.1 新增)
|
|
720
|
-
common.alertMsg('服务不可用', 0);
|
|
721
|
-
} else if (status == BMAP_STATUS_TIMEOUT) {
|
|
722
|
-
//超时。对应数值“8”。(自 1.1 新增)
|
|
723
|
-
common.alertMsg('超时', 0);
|
|
724
|
-
} else {
|
|
725
|
-
common.alertMsg('未知错误', 0);
|
|
726
|
-
}
|
|
727
|
-
}
|
|
728
|
-
*/
|