-
Notifications
You must be signed in to change notification settings - Fork 0
/
Copy pathDemo.html
482 lines (426 loc) · 18.4 KB
/
Demo.html
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
449
450
451
452
453
454
455
456
457
458
459
460
461
462
463
464
465
466
467
468
469
470
471
472
473
474
475
476
477
478
479
480
481
482
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
<title>拖拽更新经纬度_百度地图Demo</title>
<meta name="Generator" content="EditPlus">
<meta name="Author" content="">
<meta name="Keywords" content="">
<meta name="Description" content="">
</head>
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;}
.container {height:100%;width:78%;float:left;border-right:2px solid #bcbcbc;}
.a_view {height:100%;width:20%;float:left;}
.maker{ width: 93px; height: 116px; background: url(images/back.png) left top no-repeat; position: absolute; }
.badge1 {
position:relative;
}
.badge1[data-badge]:after {
content:attr(data-badge);
position:absolute;
top:3px;
right:-13px;
font-size:.7em;
background:red;
color:white;
width:24px;height:24px;
text-align:center;
line-height:24px;
border-radius:50%;
box-shadow:0 0 1px #333;
}
.testbutton {
font-family: arial;
color: #14396A !important;
font-size: 14px;
text-shadow: 1px 1px 0px #7CACDE;
box-shadow: 1px 1px 1px #BEE2F9;
padding: 8px 8px;
-moz-border-radius: 10px;
-webkit-border-radius: 10px;
border-radius: 10px;
border: 2px solid #3866A3;
background: #63B8EE;
background: linear-gradient(top, #63B8EE, #468CCF);
background: -ms-linear-gradient(top, #63B8EE, #468CCF);
background: -webkit-gradient(linear, left top, left bottom, from(#63B8EE), to(#468CCF));
background: -moz-linear-gradient(top, #63B8EE, #468CCF);
}
.testbutton:hover {
color: #14396A !important;
background: #468CCF;
background: linear-gradient(top, #468CCF, #63B8EE);
background: -ms-linear-gradient(top, #468CCF, #63B8EE);
background: -webkit-gradient(linear, left top, left bottom, from(#468CCF), to(#63B8EE));
background: -moz-linear-gradient(top, #468CCF, #63B8EE);
}
</style>
<script type="text/javascript" src="js/jquery-1.4.2.min.js"></script>
<script type="text/javascript" src="js/jquery.json-2.2.min.js"></script>
<script type="text/javascript" src="js/hash.js"></script>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=1.2"></script>
<script type="text/javascript" src="js/RichMarker.js"></script>
<script type="text/javascript" src="js/CanvasLayer.js"></script>
<script type="text/javascript" src="js/jquery.tmpl.min.js"></script>
<script id="InfoTemplate" type="text/x-jquery-tmpl">
<div style="overflow-y: hidden; font-size: 0.8em; opacity: 0.8">
<div>
<table style="width: 400px; margin-bottom: 8px; background: #f0f0f0; height: 248px" border=0 cellspacing=0 cellpadding=0>
<tbody>
<tr>
<th align=left colspan='3'>
<div style='display:inline-block;'>
<div style='display:inline-block;'>
<h2 style="text-align: center; line-height: 26px; margin: 0px; overflow: hidden; font-weight: bold">
建筑物:${name}
</h2>
</div>
</th>
</tr>
<tr>
<td style="padding-bottom: 0px; margin: 0px;width: 318px; zoom: 1;overflow: hidden;">
<img style="margin: 0px 0px; display: block" src=${photo} width=288 height=226 > </td>
<td>
<div style="border-bottom: #9cf 1px solid; border-left: #9cf 1px solid; width: 135px; height: 224px; color: #4a4a4a; font-size: 12px; overflow: hidden; border-top: #9cf 1px solid; border-right: #9cf 1px solid">
<p style="border-bottom: #9cf 1px solid; border-left: #f2f2f2 1px solid; padding-bottom: 0px; line-height: 26px; margin: 0px; padding-left: 0px; padding-right: 0px; height: 26px; overflow: hidden; border-top: #fff 1px solid; padding-top: 0px">
<span style="line-height: 26px; height: 26px; margin-left: 4px; font-weight: bold; margin-right: 4px">名字:</span>
<span style="color: #777" class='result'>${name}</span>
</p>
<p style="border-bottom: #9cf 1px solid; border-left: #f2f2f2 1px solid; padding-bottom: 0px; line-height: 26px; margin: 0px; padding-left: 0px; padding-right: 0px; height: 26px; overflow: hidden; border-top: #fff 1px solid; padding-top: 0px">
<span style="line-height: 26px; height: 26px; margin-left: 4px; font-weight: bold; margin-right: 4px">纬度:</span>
<span style="color: #777" class='result'> ${x} </span>
</p>
<p style="border-bottom: #9cf 1px solid; border-left: #f2f2f2 1px solid; padding-bottom: 0px; line-height: 26px; margin: 0px; padding-left: 0px; padding-right: 0px; height: 26px; overflow: hidden; border-top: #fff 1px solid; padding-top: 0px">
<span style="line-height: 26px; height: 26px; margin-left: 4px; font-weight: bold; margin-right: 4px">经度:</span>
<span style="color: #777" class='result'> ${y}</span>
</p>
<p style="border-bottom: #9cf 1px solid; border-left: #f2f2f2 1px solid; padding-bottom: 0px; line-height: 26px; margin: 0px; padding-left: 0px; padding-right: 4px; border-top: #fff 1px solid; padding-top: 0px">
<span style="line-height: 26px; height: 26px; margin-left: 4px; font-weight: bold; margin-right: 4px">地址:</span>
<span style="color: #777" class='result'> ${address} </span>
</p>
</div>
</td>
</tr>
</tbody>
</table>
</div>
</div>
</div>
</div>
</script>
<!-- 加载测试数据 -->
<script type="text/javascript" src="js/bulidingdatajson.js"></script>
<body>
<!-- 此处展示地图视图 -->
<div class="container" id="container"> </div>
<!-- 此处展示右侧操作栏 -->
<p>
<div id="lonlat_view"> </div>
</p>
<p>
<div style="color:blue">
<span>注1:启用编辑状态时将不会弹出详细信息窗</span>
</div>
<div style="color:blue">
<span>注2: 如果不是有效坐标,则创建中心点附近的随机坐标值</span>
</div>
<div style="color:blue">
<span>注3: 点击右侧列表可以使地图的对应元素弹出详细信息窗</span>
</div>
</p>
<p>
<div id="editCoording" style="color:red">编辑状态:禁止</div>
<div><a id='editor' class='testbutton' style='position:relative;z-index 99;margin-top:20px;margin-right:50px;top:30px;left:10px'>切换编辑状态</a></div>
</p>
<!-- 此处存放对应标注的链接 -->
<div id="a_view" class="a_view" style='margin-top:40px'>
<p>
<!-- 此处修改坐标后的反馈消息 -->
<div id="msg_view"> </div>
<br/>
建筑物列表:
</p>
</div>
</body>
<!-- 开始加载地图的处理 -->
<script type="text/javascript">
/* ****
*1、简单展示以及详细信息展示的css 样式 点击侧边栏 响应--------OK!
*2、选中点位时 弹出详细窗 --------ok!
*3、将百度地图API 版本改为V2.0 试试是否可行 --------ok!
*4、启用与禁用编辑经纬度模式 设置编辑状态时,不允许弹出详细窗体 --------ok!
*5、下方显示鼠标坐标 --------ok!
******/
var editCoording=false;
$(function(){
$("#editor").click(function(){
if(!editCoording){
alert('启用编辑坐标')
var keys=markerMap.keys(); for(var index in keys) { var marker = markerMap.get(keys[index]); marker.enableDragging(); $("#editCoording").html("编辑状态:"+"启用"); }
}else{
alert('禁用编辑坐标')
var keys=markerMap.keys();
for(var index in keys) { var marker = markerMap.get(keys[index]); marker.disableDragging(); $("#editCoording").html("编辑状态:"+"禁止"); $('#msg_view').html("保存成功!"); }
}
editCoording=!editCoording;
});
}
);
// 存储标注的hash表
var markerMap = new Hash();
//var buildingJsonStr = '[{"buildingId": "0710101842282A67E3162603F6250D67", "name": "02", "x": "31.22127", "y": "121.398516"}, {"buildingId": "07101018422820AD64EAB3AC576FFD3E", "name": "03", "x": "31.221587", "y": "121.397745"}, {"buildingId": "07101018422845B31172C77A176BDB43", "name": "04", "x": "31.220695", "y": "121.39827"}, {"buildingId": "071010184228A7EF4B0094030694C1B5", "name": "05", "x": "31.220769", "y": "121.397956"}, {"buildingId": "071010184228057205C5220A4924B99F", "name": "06", "x": "31.220178", "y": "121.398149"}, {"buildingId": "07101018422891C6FB5F3838EA67AEB6", "name": "07", "x": "31.220236", "y": "121.397776"}, {"buildingId": "071010184228E99A96B62460853A93B0", "name": "08", "x": "31.220576", "y": "121.397269"}, {"buildingId": "071010184228F4A035D852D89F418667", "name": "09", "x": "31.220638", "y": "121.396932"}, {"buildingId": "071010184228869C8C3AA796DB2D6A17", "name": "10", "x": "31.22051", "y": "121.39633"}, {"buildingId": "0710101842288CD20D0E333F3D775AEE", "name": "11", "x": "31.220634", "y": "121.395917"}, {"buildingId": "071010184228465B808DF2361EF74872", "name": "12", "x": "31.221186", "y": "121.396132"}, {"buildingId": "07101018422876B9BD4DD764214A37EF", "name": "13", "x": "31.221151", "y": "121.396478"}, {"buildingId": "07101018422860A7AEE993AD2F3D254E", "name": "14", "x": "31.221923", "y": "121.396478"}, {"buildingId": "071010184228D993F7D6F104C6241EA0", "name": "15", "x": "31.221529", "y": "121.397035"}, {"buildingId": "071010184228C77408F277C2A0FFF9EA", "name": "16", "x": "31.222487", "y": "121.39682"}, {"buildingId": "07101018422850C5CEFCC37B745AE217", "name": "17", "x": "31.222155", "y": "121.397278"}, {"buildingId": "071010184228CE5998B16072B7EADAF0", "name": "18", "x": "31.222992", "y": "121.396829"}, {"buildingId": "071010184228C4AE0885ED1B1934CAC7", "name": "19", "x": "31.223008", "y": "121.397215"}, {"buildingId": "071010184228A28019A162C9F30FB412", "name": "20", "x": "31.223054", "y": "121.397745"}, {"buildingId": "071010184228BE86B8C490400D07A7D9", "name": "21", "x": "31.223039", "y": "121.398109"}, {"buildingId": "0710101842284F721F26E60E4AE38B78", "name": "22", "x": "31.223062", "y": "121.398702"}, {"buildingId": "07101018422835C890FA62A7AC3D7B4F", "name": "23", "x": "31.223077", "y": "121.399106"}, {"buildingId": "0710101842293A343A9B0EE1A2F87ED6", "name": "24", "x": "31.222533", "y": "121.398567"}, {"buildingId": "0710101842298447B5294F823BF14B2E", "name": "25", "x": "31.222128", "y": "121.398109"}, {"buildingId": "07101018422905330D8B854F88DD30DD", "name": "26", "x": "31.221888", "y": "121.398769"}]';
//var estateJsonStr = '[{"estateId": "0705280229021824977FE8318D179397", "name": "天山河畔花园", "address": "水城路883弄/长宁路", "x": "31.221034", "y": "121.397223"}]'
/**
* 获取相关的json数组
* @returns
*/
function getJsonData() {
var reltJson;
var url = "Your url";
// 此处注释请求后台程序获取json数组的方法,直接返回定义好的json数组
// $.ajax({
// type: 'GET',
// url: url,
// async: false,
// dataType: 'json',
// timeout: 50000,
// cache: false,
// error: function(XMLHttpRequest, status, thrownError) {
// alert('Error loading ' + url +', Please send it again!');
// },
// success: function(json) {
// reltJson = json;
// }
// });
// 转换json数组字符串成json对象,正式使用时上述ajax方法直接拿到的是json对象,不需要这步操作
reltJson =buildingJsonStr;// eval(buildingJsonStr);
return reltJson;
}
// 全局变量
var map; // 地图对象
var estateJson; // 中心点json数据
var mouseoutIcon; // 鼠标移出标注时的图像对象
var mouseoverIcon; // 鼠标放在标注时的图像对象
// 绘制地图
drawMap();
/**
* 绘制地图
*/
function drawMap(){
estateJson =estateJsonStr[0];// eval(estateJsonStr)[0]; // 初始化中心点的json数据
map = new BMap.Map("container",{mapType: BMAP_HYBRID_MAP, enableHighResolution: true,enableMapClick:false}); // 设置卫星图为底图
initIcon();
configuration();
buildContextMenu();
initCenterMarker();
initOtherMarkers();
}
/**
* 地图基本功能设置
* @param map
*/
function configuration(){
// 初始化中心坐标
var point = getPoint(estateJson.y, estateJson.x);
// 启用键盘操作
map.enableKeyboard();
// 启用滚轮放大缩小
map.enableScrollWheelZoom();
// 初始化地图,设置中心点坐标和地图级别
map.centerAndZoom(point, 19);
// 添加比例尺控件
map.addControl(new BMap.ScaleControl());
// 添加平移缩放控件
map.addControl(new BMap.NavigationControl());
// 启用地图惯性拖拽
map.enableInertialDragging();
// 展示普通街道、卫星和路网的混合视图
map.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP, BMAP_HYBRID_MAP]}));
// 创建缩略地图控件
var control = new BMap.OverviewMapControl();
// 展开缩略地图控件,默认不展开
control.changeView();
// 添加缩略地图控件
map.addControl(control);
// 设置地图显示的城市 此项是必须设置的
map.setCurrentCity("上海");
map.addEventListener("mousemove",function(e){ $("#lonlat_view").html("鼠标位置:" +e.point.lng + ";" + e.point.lat); });
}
/**
* 创建右键菜单
*/
function buildContextMenu(){
var contextMenu = new BMap.ContextMenu();
var txtMenuItem = [
{
text:'放大',
callback:function(){map.zoomIn();}
},
{
text:'缩小',
callback:function(){map.zoomOut();}
},
{
text:'放置到最大级',
callback:function(){map.setZoom(20);}
},
{
text:'返回全局视图',
callback:function(){map.centerAndZoom(getPoint(estateJson.y, estateJson.x), 19);}
}
];
for(var i=0; i < txtMenuItem.length; i++){
contextMenu.addItem(new BMap.MenuItem(txtMenuItem[i].text,txtMenuItem[i].callback,100));
if(i==1 || i==3) {
contextMenu.addSeparator();
}
}
map.addContextMenu(contextMenu);
}
/**
* 初始化标注图像 useless
*/
function initIcon(){
mouseoutIcon = new BMap.Icon("images/marker-icon.gif", new BMap.Size(12, 21), {imageOffset:new BMap.Size(-46,0)});
mouseoverIcon = new BMap.Icon("images/marker-icon.gif", new BMap.Size(12, 21), {imageOffset:new BMap.Size(-69,0)});
}
/**
* 初始化中心点标注
*/
function initCenterMarker(){
addMarker2Map(estateJson);
}
/**
* 初始化其他标注
*/
function initOtherMarkers(){
// 获取其他json数据
var markersJson = getJsonData();
$.each(markersJson, function(){
addMarker2Map(this);
});
}
/**
* 获取栋座坐标point对象
* 无坐标时,随机生成在小区坐标附近
* @param y
* @param x
*/
function getPoint(y, x){
// 因为json字符串中定义的是字符串,需要转换成float
var _y = parseFloat(y);
var _x = parseFloat(x);
// 如果是有效的坐标,则直接创建point
if(_y > 0 || _x > 0)
return new BMap.Point(_y, _x);
// 如果不是有效坐标,则创建中心点附近的随机坐标值 (good)
var bounds = map.getBounds();
var sw = bounds.getSouthWest();
var ne = bounds.getNorthEast();
var lngSpan = Math.abs(sw.lng - ne.lng);
var latSpan = Math.abs(ne.lat - sw.lat);
return new BMap.Point(sw.lng + lngSpan * (Math.random() * 0.8), ne.lat - latSpan * (Math.random() * 0.8));
}
/**
* 添加多个标注
* @param point 坐标
* @param text 标注文本
* @param buildingId 编号
*/
function addMarker2Map(markersJson){
var point= getPoint(markersJson.y, markersJson.x);
var text=markersJson.name;
var id=markersJson.buildingId;
var photo=markersJson.photo;
// 创建标注 // 圆角图形 82 102
var rrDOM = "<div id='"+ id +"' class='maker badge1' data-badge='" + text +"'"
+ "style='width:82px;height:102px;background:url(images/icon.png) left top no-repeat; position: absolute;'>"
+ "<div style='width:65px;height:65px; margin-top:7px;margin-left:9px;border-radius:50%; overflow:hidden;'><img src='images/small.jpg' /></div>"
+ "</div>";
var marker = new BMapLib.RichMarker(rrDOM,point,{ "anchor" : new BMap.Size(-41, -102), "disableDragging" : true});
var popContent=$('#InfoTemplate').tmpl(markersJson).html();//引用详细弹出窗模板
var myinfoWindow1 = new BMap.InfoWindow(popContent, { offset: new BMap.Size(0, -0) });
marker.setInfoWindow(myinfoWindow1);
// 标注拖拽结束后绑定的事件
marker.addEventListener("ondragend", function(e){
editCoord(id, e.point.lat, e.point.lng, text);
});
// 鼠标离开标注时绑定的事件
marker.addEventListener("onmouseout", function(e){
//marker.setIcon(mouseoutIcon);
});
// 鼠标移到标注时绑定的事件
marker.addEventListener("onmouseover", function(e){
//marker.setIcon(mouseoverIcon);
//this.openInfoWindow(infoWindow);
});
// 鼠标点击标注时绑定的事件
marker.addEventListener("onclick", function(e){
console.log('sdsadsa');
if(!editCoording){
this.openInfoWindow();
}
});
// 信息窗关闭时绑定的事件
marker.addEventListener("infowindowclose", function(e){
//marker.setIcon(mouseoutIcon);
});
// 信息窗打开时绑定的事件
marker.addEventListener("infowindowopen", function(e){
//marker.setIcon(mouseoverIcon);
});
// 显示到地图中
map.addOverlay(marker);
// 存储到hash
markerMap.put(id, marker);
var link = '<li><a href="javascript:void(0);" onclick="showInfoWindos(\''+id+'\')">'+ text +'</a></li>';
$('#a_view').append(link);
}
/**
* 展示标注的信息窗
* @param id
*/
function showInfoWindos(id){
var marker = markerMap.get(id);
if(marker.getInfoWindow() != undefined){
marker.openInfoWindow();
}
}
/**
* 保存坐标编辑
* @param id
* @param x
* @param y
* @param name
*/
function editCoord(id, x, y, name){
// $.ajax({
// type: 'POST',
// url: 'Your url',
// data: {
// 'id' : id,
// 'x' : x,
// 'y' : y
// },
// success: function(data) {
// showMsg("<strong>" + name + "修改后的坐标已自动保存</strong>");
// }
// });
showMsg('<strong>"' + name + '"修改后的坐标已自动保存</strong>');
}
/**
* 获取信息窗html
*/
function getContent(name){
var content = "<div><p>{name}</p></div>";
return content.replace("{name}", name);
}
/**
* 展示坐标修改后的提醒内容
*/
function showMsg(msg){
$('#msg_view').html(msg);
}
</script>
</html>