-
Notifications
You must be signed in to change notification settings - Fork 48
Expand file tree
/
Copy pathAreaRestriction.js
More file actions
150 lines (130 loc) · 4.51 KB
/
Copy pathAreaRestriction.js
File metadata and controls
150 lines (130 loc) · 4.51 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
/**
* @fileoverview 百度地图浏览区域限制类,对外开放。
* 允许开发者输入限定浏览的地图区域的Bounds值,
* 则地图浏览者只能在限定区域内浏览地图。
* 基于Baidu Map API 1.2。
*
* @author Baidu Map Api Group
* @version 1.2
*/
/**
* @namespace BMap的所有library类均放在BMapLib命名空间下
*/
var BMapLib = window.BMapLib = BMapLib || {};
(function() {
/**
* @exports AreaRestriction as BMapLib.AreaRestriction
*/
var AreaRestriction =
/**
* AreaRestriction类,静态类,不用实例化
* @class AreaRestriction类提供的都是静态方法,勿需实例化即可使用。
*/
BMapLib.AreaRestriction = function(){
}
/**
* 是否已经对区域进行过限定的标识
* @private
* @type {Boolean}
*/
var _isRestricted = false;
/**
* map对象
* @private
* @type {BMap}
*/
var _map = null;
/**
* 开发者需要限定的区域
* @private
* @type {BMap.Bounds}
*/
var _bounds = null;
/**
* 对可浏览地图区域的限定方法
* @param {BMap} map map对象
* @param {BMap.Bounds} bounds 开发者需要限定的区域
*
* @return {Boolean} 完成了对区域的限制即返回true,否则为false
*/
AreaRestriction.setBounds = function(map, bounds){
// 验证输入值的合法性
if (!map ||
!bounds ||
!(bounds instanceof BMap.Bounds)) {
throw "请检查传入参数值的合法性";
return false;
}
if (_isRestricted) {
this.clearBounds();
}
_map = map;
_bounds = bounds;
// 添加地图的moving事件,用以对浏览区域的限制
_map.addEventListener("moveend", this._mapMoveendEvent);
_isRestricted = true;
return true;
};
/**
* 需要绑定在地图移动事件中的操作,主要控制出界时的地图重新定位
* @param {Event} e e对象
*
* @return 无返回值
*/
AreaRestriction._mapMoveendEvent = function(e) {
// 如果当前完全没有出界,则无操作
if (_bounds.containsBounds(_map.getBounds())) {
return;
}
// 两个需要对比的bound区域的边界值
var curBounds = _map.getBounds(),
curBoundsSW = curBounds.getSouthWest(),
curBoundsNE = curBounds.getNorthEast(),
_boundsSW = _bounds.getSouthWest(),
_boundsNE = _bounds.getNorthEast();
// 需要计算定位中心点的四个边界
var boundary = {n : 0, e : 0, s : 0, w : 0};
// 计算需要定位的中心点的上方边界
boundary.n = (curBoundsNE.lat < _boundsNE.lat) ?
curBoundsNE.lat :
_boundsNE.lat;
// 计算需要定位的中心点的右边边界
boundary.e = (curBoundsNE.lng < _boundsNE.lng) ?
curBoundsNE.lng :
_boundsNE.lng;
// 计算需要定位的中心点的下方边界
boundary.s = (curBoundsSW.lat < _boundsSW.lat) ?
_boundsSW.lat :
curBoundsSW.lat;
// 计算需要定位的中心点的左边边界
boundary.w = (curBoundsSW.lng < _boundsSW.lng) ?
_boundsSW.lng :
curBoundsSW.lng;
// 设置新的中心点
var center = new BMap.Point(boundary.w + (boundary.e - boundary.w) / 2,
boundary.s + (boundary.n - boundary.s) / 2);
if (this.tempCenter
&& this.tempCenter.lng === center.lng
&& this.tempCenter.lat === center.lat) {
return;
} else {
this.tempCenter = {};
this.tempCenter.lng = center.lng;
this.tempCenter.lat = center.lat;
}
setTimeout(function() {
_map.panTo(center, {noAnimation : "no"});
}, 1);
};
/**
* 清除对地图浏览区域限定的状态
* @return 无返回值
*/
AreaRestriction.clearBounds = function(){
if (!_isRestricted) {
return;
}
_map.removeEventListener("moveend", this._mapMoveendEvent);
_isRestricted = false;
};
})();