AMap_wev8.js
18 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
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
388
389
390
391
392
393
394
395
396
397
398
399
400
401
402
403
404
405
406
407
408
409
410
411
412
413
414
415
416
417
418
419
420
421
422
423
424
425
426
427
428
429
430
431
432
433
434
435
436
437
438
439
440
441
442
443
444
445
446
447
448
define(['mUtil', "Component", "amap"], function (mUtil, Component) {
var Map = function (options) {
var _amap = {}, amapInstance, $mapContainer, additionalPoints = {};
Component.super(this, options);
this.type = "AMap";
this.tpl = this.type + "_html";
this.css = this.type + "_css";
var currLocStr = "CURRENT_LOCATION",
CURR_POS = '/mobilemode/mobile/images/plugin/current-location.png',
START_ICON = '/mobilemode/mobile/images/plugin/start.png',
MID_ICON = '/mobilemode/mobile/images/plugin/mid.png',
END_ICON = '/mobilemode/mobile/images/plugin/end.png';
var vm = this.viewModel = {
type: '2',
centerPoint: {
address: currLocStr,
coordinateType: -1, //坐标类型 1:高德,腾讯地图类的坐标系。2:百度地图类的坐标系。3:gps类的坐标系
},
additionalPointType: 1,
apiConfig: {
api: {},
params: {
query: [],
body: {}
}
},
additionalPoint: {
enabled: false,
coordinateType: 1, //坐标类型 1:高德,腾讯地图类的坐标系。2:百度地图类的坐标系。3:gps类的坐标系
additionalRadius: '',
coordinates: [],
showLabel: true,
isConnection: 0
},
height: 200, //高度
zoomLevel: 18, //比例尺
standalone: true
};
this.beforeMount = function () {
if (/^\d+%$/g.test(vm.height)){
var pageHeight = this.$container.height();
var percent = parseFloat(vm.height) / 100;
vm.realHeight = pageHeight * percent;
} else if (mUtil.isFunction(vm.height)) {
vm.height = vm.height();
} else {
vm.height = parseFloat(vm.height);
}
if (!vm.realHeight){
vm.realHeight = vm.height;
}
};
this.mounted = function () {
$mapContainer = $('.wev-comp-' + this.type + ' > div', this.$el);
var type = vm.type;
var address = vm.centerPoint.address;
var coordinateType = vm.centerPoint.coordinateType;
var createMapAndAddPoint = function (p) {
var amap = amapInstance = new AMap.Map($mapContainer[0], {
resizeEnable: true, //是否监控地图容器尺寸变化
zoom: vm.zoomLevel,
center: p
});
if(vm.type !== '3'){
var centerIcon = CURR_POS;
var opts = {};
if(vm.centerIcon && vm.iconType == '1'){
centerIcon = vm.centerIcon;
}
if(vm.additionalPoint.isConnection == '3'){
opts = {
size: new AMap.Size(19, 30),
image: START_ICON,
imageSize: new AMap.Size(19, 30),
imageOffset: new AMap.Pixel(0, 0)
};
}
amap.add(new AMap.Marker({
position: p,
icon: new AMap.Icon($.extend({
size: new AMap.Size(40, 40),
image: centerIcon,
imageSize: new AMap.Size(40, 40),
imageOffset: new AMap.Pixel(0, 0)
}, opts)),
offset: new AMap.Pixel(-20, -35)
}));
}
//获取附加点
if (vm.additionalPoint.enabled) {
_amap.getAdditionalPoints({lng: p[0], lat: p[1]});
var callback = function(e){
var dataid = $(this).data("id");
$mapContainer.find(".amap-marker-label").removeClass("selected");
$(this).closest(".amap-marker-label").addClass("selected");
var clickFunc = vm.additionalPoint.click;
clickFunc && clickFunc(additionalPoints[dataid]);
e && e.stopPropagation();
};
$mapContainer.on("click", ".amap-label-content", function(e){
callback.call(this, e);
});
$mapContainer.on("touchstart", ".amap-label-content", function(e){
callback.call(this, e);
});
}
};
_amap.getCenterPointCoordinate(type, address, coordinateType, createMapAndAddPoint);
};
_amap.addMarker = function (data, coordinateType, callback, opts) {
var that = this;
if (!data || !data.lng || !data.lat) {
mUtil.getLabel(5296, "经纬度:#ADDR# 格式错误!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",address));
});
return ;
}
var option = $.extend({}, opts);
_amap.convertCoordinate([data.lng, data.lat], coordinateType, function (point) {
var pMarker = new AMap.Marker({
position: point,
icon: new AMap.Icon($.extend({
size: new AMap.Size(30, 30),
image: '/mobilemode/mobile/images/plugin/location.png',
imageSize: new AMap.Size(30, 30),
imageOffset: new AMap.Pixel(0, 0)
}, option)),
offset: new AMap.Pixel(-15, -25)
});
amapInstance.add(pMarker);
pMarker.on("click", function(){
if(this.Mg){
$mapContainer.find(".amap-marker-label").removeClass("selected");
$(this.Mg.contentDom).siblings(".amap-marker-label").addClass("selected");
}
callback && mUtil.eval(callback, that.pageid, data);
});
if (!vm.additionalPoint.showLabel) return;
var uuid = mUtil.UUID();
additionalPoints[uuid] = data;
if(data.name){
pMarker.setLabel({
offset: (option && option.labelOffset) || new AMap.Pixel(15, -25),
content: "<div class='amap-label-content' data-id='"+uuid+"'>"+data.name+"</div>"
});
}
});
};
_amap.addMarkers = function (datas, coordinateType, callback) {
if(vm.additionalPoint.isConnection == '3'){
var opts = {
size: new AMap.Size(19, 30),
imageSize: new AMap.Size(19, 30),
imageOffset: new AMap.Pixel(0, 0),
labelOffset: new AMap.Pixel(10, -25)
};
_amap.addMarker(datas[datas.length - 1], coordinateType, vm.additionalPoint.click, $.extend(opts, {image: END_ICON}));
var tempdatas = datas.slice(0, datas.length - 1);
if(vm.type == '3' && datas.length > 1){
_amap.addMarker(datas[0], coordinateType, vm.additionalPoint.click, $.extend(opts, {image: START_ICON}));
tempdatas = datas.slice(1, datas.length - 1);
}
mUtil.isArray(tempdatas) && tempdatas.forEach(function (item) {
_amap.addMarker(item, coordinateType, vm.additionalPoint.click, $.extend(opts, {image: MID_ICON}));
});
} else {
mUtil.isArray(datas) && datas.forEach(function (item) {
_amap.addMarker(item, coordinateType, vm.additionalPoint.click);
});
}
if(vm.additionalPoint.isConnection != '0'){
_amap.connectAdditionalPoints(datas, coordinateType);
}
};
_amap.convertCoordinate = function (transPoint, coordinateType, callback) {
if (coordinateType === 1) { //高德,腾讯地图类的坐标系
callback && callback(transPoint);
} else{
require(["amapHelper"], function(amapHelper){
amapHelper.convertPoint(transPoint, coordinateType, function(convertPoint){
callback && callback(convertPoint);
});
});
}
};
_amap.getCenterPointCoordinate = function (type, address, coordinateType, callback) {
if (coordinateType == -1) { //中文地址
if (address == currLocStr || type == '3') { //当前位置
return Mobile_NS.getCurrPosition(function (result) {
vm.centerPoint.lng = result.lng;
vm.centerPoint.lat = result.lat;
if(typeof callback == 'function'){
callback([result.lng, result.lat]);
}
});
}
require(["amapHelper"], function(amapHelper){
amapHelper.getPosition(address,function (point) {
vm.centerPoint.lng = point[0];
vm.centerPoint.lat = point[1];
if(typeof callback == 'function'){
callback(point);
}
});
});
} else {
var arr = address.split(",");//保存经纬度数组
if (arr.length != 2) {
mUtil.getLabel(5296, "经纬度:#ADDR# 格式错误!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",address));
});
return;
}
_amap.convertCoordinate([arr[0], arr[1]], coordinateType, function (point) {
vm.centerPoint.lng = point[0];
vm.centerPoint.lat = point[1];
if(typeof callback == 'function'){
callback(point);
}
});
}
}
_amap.getAdditionalPoints = function (p) {
if(vm.additionalPointType == '1'){
if (!vm.standalone) {
var actionUrl = mUtil.getActionUrl(this.type, { action: "getAdditionalPoints", mec_id: this.id, lng: p.lng, lat: p.lat,additionalRadius:vm.additionalPoint.additionalRadius }, this.pageid);
return mUtil.getJSON(actionUrl, function (result) {
_amap.addMarkers(result.datas, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
});
}
var coordinates = vm.additionalPoint.coordinates;
if (mUtil.isFunction(coordinates)) {
return coordinates(p, function (datas) {
_amap.addMarkers(datas, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
});
}
} else if (vm.additionalPointType == '2'){
require(["apiHelper"], function(apiHelper){
queryParams = {};
var query = vm.apiConfig.params.query;
query.forEach(function(item){
queryParams[item.name] = item.value;
});
apiHelper.callApi(vm.apiConfig, queryParams, function(result){
if(result.status == '1'){
var datas = [];
var getDistance = function(center, p){
var earthRadius = 6378137;
var radLat1 = (center[1] * Math.PI / 180.0);
var radLat2 = (p[1] * Math.PI / 180.0);
var a = radLat1 - radLat2;
var b = (center[0] * Math.PI / 180.0) - (p[0] * Math.PI / 180.0);
var s = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin(a/2),2) + Math.cos(radLat1)*Math.cos(radLat2)*Math.pow(Math.sin(b/2),2)));
s = s * earthRadius;
s = Math.round(s * 10000) / 10000;
return s;
};
result.data.reduce(function (prev, item) {
return prev.then(function () {
var done = $.Deferred();
if(vm.type != '3' && vm.additionalPoint.additionalRadius != ''){
_amap.convertCoordinate([item.lng, item.lat], vm.additionalPoint.coordinateType, function(p){
if(getDistance([vm.centerPoint.lng, vm.centerPoint.lat], p) <= vm.additionalPoint.additionalRadius){
datas.push(item);
}
done.resolve();
});
} else {
datas.push(item);
done.resolve();
}
return done;
});
}, $.Deferred().resolve()).then(function () {
_amap.addMarkers(datas, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
});
}
}, function(errMsg){
console.error(errMsg);
});
});
}
};
_amap.connectAdditionalPoints = function(points, addiCoordinateType) {
var coordinates = [];
if(vm.type != '3'){
coordinates.push([vm.centerPoint.lng, vm.centerPoint.lat]);
}
function drawRoute (route) {
var path = parseRouteToPath(route)
var routeLine = new AMap.Polyline({
path: path,
isOutline: true,
outlineColor: '#ffeeee',
borderWeight: 2,
strokeWeight: 5,
strokeColor: '#0091ff',
lineJoin: 'round'
})
routeLine.setMap(amapInstance)
}
// 解析Route对象,构造成AMap.Polyline的path参数需要的格式
function parseRouteToPath(route) {
var path = []
for (var i = 0, l = route.steps.length; i < l; i++) {
var step = route.steps[i]
for (var j = 0, n = step.path.length; j < n; j++) {
path.push(step.path[j])
}
}
return path
}
if(mUtil.isArray(points)){
points.reduce(function(prev, item){
return prev.then(function(){
var done = $.Deferred();
_amap.convertCoordinate([item.lng, item.lat], addiCoordinateType, function(point){
coordinates.push(point);
done.resolve();
});
return done;
});
}, $.Deferred().resolve()).then(function(){
if(coordinates.length < 2){
return;
}
if(vm.additionalPoint.isConnection == '1'){
var path = [];
coordinates.forEach(function(item){
var i = {
O : item[1],
P : item[0],
lat : item[1],
lng : item[0]
}
path.push(i);
});
var routeLine = new AMap.Polyline({
path: path,
isOutline: true,
outlineColor: '#ffeeee',
borderWeight: 2,
strokeWeight: 5,
strokeColor: '#0091ff',
lineJoin: 'round'
})
routeLine.setMap(amapInstance)
} else if(vm.additionalPoint.isConnection == '2'){
AMap.plugin('AMap.Walking', function() {
// 步行导航
var walking = new AMap.Walking({
autoFitView: true
});
coordinates.reduce(function(prev, curr){
walking.search(prev, curr, function(status, result) {
// result即是对应的步行路线数据信息,相关数据结构文档请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_WalkingResult
if (status === 'complete') {
if (result.routes && result.routes.length) {
drawRoute(result.routes[0])
}
} else {
console.error('步行路线数据查询失败' + result)
}
});
return curr;
});
});
} else if(vm.additionalPoint.isConnection == '3'){
AMap.service('AMap.Driving', function() {
var driving = new AMap.Driving({
map: amapInstance,
autoFitView: true,
hideMarkers: true
});
var startPoint = coordinates[0];
var endPoint = coordinates[coordinates.length - 1];
var wayPoints = coordinates.slice(1, coordinates.length - 1);
driving.search(startPoint, endPoint, {waypoints: wayPoints}, function(status, result) {
// result 即是对应的驾车导航信息,相关数据结构文档请参考 https://lbs.amap.com/api/javascript-api/reference/route-search#m_DrivingResult
if (status !== 'complete') {
console.error('获取驾车数据失败:' + result)
}
});
});
}
});
}
}
_amap.getAdditionalPoints = _amap.getAdditionalPoints.bind(this);
_amap.addMarker = _amap.addMarker.bind(this);
};
return Component.init(Map);
});