Map_wev8.js
5.88 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
define(['mUtil', "Component", "baidumap"], function (mUtil, Component) {
var Map = function (options) {
var _map = {};
Component.super(this, options);
this.type = "Map";
this.tpl = this.type + "_html";
this.css = this.type + "_css";
var currLocStr = "CURRENT_LOCATION";
var vm = this.viewModel = {
centerPoint: {
address: currLocStr,
coordinateType: -1, //坐标类型 1:高德,腾讯地图类的坐标系。2:百度地图类的坐标系。3:gps类的坐标系
},
additionalPoint: {
enabled: false,
coordinateType: 2, //坐标类型 1:高德,腾讯地图类的坐标系。2:百度地图类的坐标系。3:gps类的坐标系
coordinates: [],
showLabel: true
},
height: 200, //高度
zoomLevel: 18, //比例尺
standalone: true
};
this.beforeMount = function () {
if (vm.height == "100%") {
vm.height = this.$container.height();
} else if (mUtil.isFunction(vm.height)) {
vm.height = vm.height();
} else {
vm.height = parseFloat(vm.height);
}
};
this.mounted = function () {
var $mapContainer = $('.wev-comp-' + this.type + ' > div', this.$el);
var map = vm.map = new BMap.Map($mapContainer[0]);
map.clearOverlays();
map.enableScrollWheelZoom(true);
var address = vm.centerPoint.address;
var coordinateType = vm.centerPoint.coordinateType;
var setPoint = function (p) {
map.centerAndZoom(p, vm.zoomLevel);
map.addOverlay(new BMap.Marker(p, {
icon: new BMap.Icon("/mobilemode/mobile/images/plugin/current-location.png", new BMap.Size(40, 40), { imageSize: new BMap.Size(40, 40) })
}));
//获取附加点
if (vm.additionalPoint.enabled) {
_map.getAdditionalPoints(p);
}
};
if (coordinateType == -1) { //中文地址
if (address == currLocStr) { //当前位置
return Mobile_NS.getCurrentPosition(function (result) {
var p = new BMap.Point(result.lng, result.lat);
setPoint(p);
});
}
var searchCompleted = false;
var local = new BMap.LocalSearch(map, {
onSearchComplete: function () {
if (searchCompleted) { return; }
var results = local.getResults();
if (!results || results.getNumPois() <= 0) {
mUtil.getLabel(5297, "地址:#ADDR# 没有解析到结果!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",address));
});
return ;
}
searchCompleted = true;
setPoint(results.getPoi(0).point);
}
});
return local.search(address);
}
var arr = address.split(",");//保存经纬度数组
if (arr.length != 2) {
mUtil.getLabel(5296, "经纬度:#ADDR# 格式错误!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",address));
});
return ;
}
_map.convertCoordinate(new BMap.Point(arr[0], arr[1]), coordinateType, function (point) {
setPoint(point);
});
};
_map.addMarker = function (data, coordinateType, callback) {
var that = this;
if (!data || !data.lng || !data.lat) {
mUtil.getLabel(5296, "经纬度:#ADDR# 格式错误!",function(msg){
mUtil.console.error(msg.replace("#ADDR#",address));
});
return ;
}
var myIcon = new BMap.Icon("/mobilemode/mobile/images/plugin/location.png", new BMap.Size(30, 30), {
imageSize: new BMap.Size(30, 30)
});
var doCallback = function () {
if (!mUtil.isFunction(callback)) return;
mUtil.eval(callback, that.pageid, data);
};
_map.convertCoordinate(new BMap.Point(data.lng, data.lat), coordinateType, function (point) {
var pMarker = new BMap.Marker(point, { icon: myIcon });
vm.map.addOverlay(pMarker);
pMarker.addEventListener("click", function () {
doCallback();
window.event.stopPropagation();
});
if (!vm.additionalPoint.showLabel) return;
var pLabel = new BMap.Label(data.label, { offset: new BMap.Size(15, -25) });
pMarker.setLabel(pLabel);
pLabel.addEventListener("click", function () {
var that = $(this.V);
that.parent("span.BMap_Marker").siblings("span.BMap_Marker").find("label.BMapLabel").removeClass("selected");
that.addClass("selected");
doCallback();
window.event.stopPropagation();
});
});
};
_map.addMarkers = function (datas, coordinateType, callback) {
mUtil.isArray(datas) && datas.forEach(function (item) {
_map.addMarker(item, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
});
};
_map.convertCoordinate = function (transPoint, coordinateType, callback) {
var convertor = new BMap.Convertor();
if (coordinateType == 1) { //高德,腾讯地图类的坐标系
convertor.translate([transPoint], 3, 5, function (data) {
mUtil.isFunction(callback) && callback(data.points[0]);
});
} else if (coordinateType == 2) { //百度地图类的坐标系
mUtil.isFunction(callback) && callback(transPoint);
} else if (coordinateType == 3) { //gps类的原始坐标系
convertor.translate([transPoint], 1, 5, function (data) {
mUtil.isFunction(callback) && callback(data.points[0]);
});
}
};
_map.getAdditionalPoints = function (p) {
if (!vm.standalone) {
var actionUrl = mUtil.getActionUrl(this.type, { action: "getAdditionalPoints", mec_id: this.id, lng: p.lng, lat: p.lat }, this.pageid);
return mUtil.getJSON(actionUrl, function (result) {
_map.addMarkers(result.datas, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
});
}
var coordinates = vm.additionalPoint.coordinates;
if (mUtil.isFunction(coordinates)) {
return coordinates(p, function (datas) {
_map.addMarkers(datas, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
});
}
_map.addMarkers(coordinates, vm.additionalPoint.coordinateType, vm.additionalPoint.click);
};
_map.getAdditionalPoints = _map.getAdditionalPoints.bind(this);
_map.addMarker = _map.addMarker.bind(this);
};
return Component.init(Map);
});