bdmap.js
5.14 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
/**
* simple baidu map
* Code in 2014/10/14
* By van
*/
define( 'bdmap' ,function( require, exports, module ){
require( 'fancybox' );
var bdmap = function( target, options ){
this.target = target;
this.o = {
container : "showmap", //显示地图容器ID
sWidth: 290, //地址窗口宽度
sHeight: 36, //地址窗口高度
locate : "locate", //获取地址数据的标签属性
now: false, //设置即时显示在当前页面
where: [] //立即生成传入的坐标
}
this.o = $.extend( this.o,options );
this.init();
};
bdmap.prototype = {
init: function(){
//赋予地图对象
window.map = window.map || new BMap.Map( this.o.container ), // 创建Map实例
window.geoc = window.geoc || new BMap.Geocoder(); //实例化地图解析器
this.makeMap( [104.072236,30.663439] );
this.setMap();
//this.clickbtn();
this.getfancy();
},
getfancy: function(){
$( this.target ).fancybox({
padding: 0
});
},
setMap: function(){
var _this = this, data, point;
$( this.target ).click(function(){
window.map.clearOverlays();
data = _this.getData( $(this) );
if ( _this.testZh( data[0] ) ){
_this.getpoint(
window.map,
data
);
}else {
point = new BMap.Point( data[0], data[1] );
_this.getlocation(
window.map,
point
);
}
});
},
makeMap: function( data ){
var _this = this,
point = new BMap.Point( data[0], data[1] );
window.map.centerAndZoom( point, 16 ); // 初始化地图,设置中心点坐标和地图级别
window.map.setCurrentCity("成都"); // 设置地图显示的城市 此项是必须设置的
window.map.enableScrollWheelZoom(true); //设置鼠标缩放功能
window.map.addControl(
new BMap.ScaleControl({
anchor: BMAP_ANCHOR_TOP_RIGHT
})
); //添加缩放平移控件
window.map.addControl(
new BMap.NavigationControl({
anchor: BMAP_ANCHOR_TOP_RIGHT
})
); //添加缩放平移控件
//清空所有覆盖物
window.map.clearOverlays();
//依据参数立即生成覆盖层
if ( _this.o.now && _this.o.where.length > 0 ){
var searchInfoWindow = null;
searchInfoWindow = _this.openCover(
window.map,
_this.o.where
);
};
},
getData: function( target ){
//获取地址信息
return locate = $( target )
.attr( this.o.locate )
.split( /[,;]/ );
},
testZh: function( o ){
//检测传入字符中是否含有中文
return /[\u4e00-\u9fa5]/.test( o );
},
getpoint: function( map, data ){
//传入真实街道地址
var _this = this;
return window.geoc.getPoint(
data[0],
function( point ){
map.centerAndZoom( point, 16 );
var searchInfoWindow = null;
searchInfoWindow = _this.openCover(
window.map,
[ data[0], data[1] ]
);
//添加标注并显示信息窗口
var marker = new BMap.Marker( point );
map.addOverlay( marker );
searchInfoWindow.open( marker );
}
);
},
getlocation: function( map, point ){
//传入百度坐标
var _this = this;
return window.geoc.getLocation(
point,
function( rs ){
var searchInfoWindow = null;
searchInfoWindow = _this.openCover(
map,
[ rs.address, rs.business ]
);
//添加标注并显示信息窗口
var marker = new BMap.Marker( point ); //创建marker对象
map.addOverlay( marker ); //在地图中添加marker
searchInfoWindow.open( marker );
}
);
},
openCover: function( map, arr ){
var _this = this;
return new BMapLib.SearchInfoWindow(
map,
arr[0],
{
title: arr[1],
width: _this.o.sWidth,
height: _this.o.sHeight,
panel: "panel",
enableAutoPan: true,
searchTypes :[
BMAPLIB_TAB_SEARCH, //周边检索
BMAPLIB_TAB_TO_HERE, //到这里去
BMAPLIB_TAB_FROM_HERE //从这里出发
]
}
);
}
};
return bdmap;
});