赖素文
authored
|
1
|
;(function (window) {
|
赖素文
authored
|
2
3
4
5
|
var sysMapObj = null,
sysInfoWindowAll = {},
sysZoomNumber=12,
sysPointKey = "_point";
|
赖素文
authored
|
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
|
var mapApp = function (selector, lon = 113.34693, lat = 28.21849) {
return new mapApp.fn.init(selector, lon, lat);
}
mapApp.fn = mapApp.prototype = {
constructor: mapApp,
init: function (selector, lon, lat) {
this.selector = selector;
this.lon = lon;
this.lat = lat;
this.initMap = function () {
return this.initMapMethod();
}
|
赖素文
authored
|
22
|
this.createMarkerInfo = function (message, obj) {
|
赖素文
authored
|
23
|
return this.markerInfoMethod(message, obj)
|
赖素文
authored
|
24
|
}
|
赖素文
authored
|
25
26
27
28
29
30
31
|
this.getSysMapObj = function () {
return sysMapObj;
}
this.getLocateMarker = function (hhMarkerId) {
return this.getLocateMarkerMethod(hhMarkerId)
}
|
赖素文
authored
|
32
33
|
},
|
赖素文
authored
|
34
|
initMapMethod() {
|
赖素文
authored
|
35
36
37
38
39
40
|
if (document.querySelector("#" + this.selector).length == 0) {
alert(`初始化方法元素节点${"#" + this.selector}不存在!`);
return;
}
sysMapObj = new BMapGL.Map(this.selector);
|
赖素文
authored
|
41
|
sysMapObj.centerAndZoom(this.getPoint(), sysZoomNumber); //设置中心点
|
赖素文
authored
|
42
43
44
45
|
sysMapObj.enableScrollWheelZoom(true); // //开启鼠标滚轮缩放
var scaleCtrl = new BMapGL.ScaleControl(); // 添加比例尺控件
sysMapObj.addControl(scaleCtrl);
|
赖素文
authored
|
46
|
|
赖素文
authored
|
47
48
|
var zoomCtrl = new BMapGL.ZoomControl(); // 添加缩放控件
sysMapObj.addControl(zoomCtrl);
|
赖素文
authored
|
49
|
|
赖素文
authored
|
50
51
52
|
var cityCtrl = new BMapGL.CityListControl(); // 添加城市列表控件
sysMapObj.addControl(cityCtrl);
|
赖素文
authored
|
53
54
55
|
var locationCtrl = new BMapGL.LocationControl({
anchor: BMAP_ANCHOR_BOTTOM_RIGHT,
offset: new BMapGL.Size(10, 80)
|
赖素文
authored
|
56
57
|
});;
sysMapObj.addControl(locationCtrl);// 添加定位控件
|
赖素文
authored
|
58
59
60
|
//sysMapObj.setHeading(64.5);
//sysMapObj.setTilt(73);
|
赖素文
authored
|
61
62
|
return this;
},
|
赖素文
authored
|
63
64
65
66
67
|
/**
* 创建标注和信息框
* obj { title: x, longitude: longitude, latitude:latitude }
*/
markerInfoMethod: function (message, obj) {
|
赖素文
authored
|
68
69
70
71
|
let point = this.getPoint(obj.longitude, obj.latitude),
marker = new BMapGL.Marker(point), // 创建标注
markerId = obj.id.toString(); //自定义标注唯一ID
marker.hhId = markerId;
|
赖素文
authored
|
72
|
sysMapObj.addOverlay(marker); // 将标注添加到地图中
|
赖素文
authored
|
73
|
|
赖素文
authored
|
74
|
var opts = {
|
赖素文
authored
|
75
|
width: 300, // 信息窗口宽度
|
赖素文
authored
|
76
|
height: 100, // 信息窗口高度
|
赖素文
authored
|
77
|
title: obj.title, // 信息窗口标题
|
赖素文
authored
|
78
|
}
|
赖素文
authored
|
79
|
let infoWindow = new BMapGL.InfoWindow(message, opts); // 创建信息窗口对象
|
赖素文
authored
|
80
|
sysMapObj.openInfoWindow(infoWindow, point); //开启信息窗口
|
赖素文
authored
|
81
82
83
84
85
|
if (!sysInfoWindowAll[markerId]) {
sysInfoWindowAll[markerId] = infoWindow;
sysInfoWindowAll[markerId + sysPointKey] = point;
}
|
赖素文
authored
|
86
87
88
89
90
91
92
|
marker.addEventListener("click", function () {
sysMapObj.openInfoWindow(infoWindow, point); //开启信息窗口
});
return this;
},
|
赖素文
authored
|
93
94
95
96
|
getPoint(lon, lat) {
var longitude = lon ?? this.lon;
var latitude = lat ?? this.lat;
var point = new BMapGL.Point(longitude, latitude);
|
赖素文
authored
|
97
|
return point;
|
赖素文
authored
|
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
|
},
/**
* 地址解析经纬度
*/
getPointByAddress(address, successCallBack, errorCallBack) {
//创建地址解析器实例
var myGeo = new BMapGL.Geocoder();
// 将地址解析
myGeo.getPoint(address, function (point) {
if (point) {
successCallBack(point)
return;
}
successCallBack(null)
alert('您输入的地址没有解析到结果!');
}, function (err) {
alert("地址解析异常:", err);
})
},
|
赖素文
authored
|
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
|
/**
* 查找Marker并打开对应的infoWindow信息框
*/
getLocateMarkerMethod(markerId) {
var overlays = sysMapObj.getOverlays();
for (var i = 0; i < overlays.length; i++) {
var overlay = overlays[i];
if (overlay.hhId == markerId) {
var pos = overlay.getPosition();
sysMapObj.panTo(pos); // 定位到 Marker
overlay.setAnimation(BMAP_ANIMATION_BOUNCE); // 添加动画效果
let infoWindow = sysInfoWindowAll[markerId];
if (infoWindow) {
sysMapObj.centerAndZoom(pos, sysZoomNumber); //设置中心点
let point = sysInfoWindowAll[markerId + sysPointKey];
sysMapObj.openInfoWindow(infoWindow, point); // 打开对应的 infoWindow
}
break;
}
}
}
|
赖素文
authored
|
140
141
142
143
144
145
|
}
mapApp.fn.init.prototype = mapApp.fn;
window.mapApp = mapApp;
})(window, undefined);
|