提交 | 用户 | 时间
|
1ac2bc
|
1 |
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" |
懒 |
2 |
"http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd"> |
|
3 |
<html xmlns="http://www.w3.org/1999/xhtml"> |
|
4 |
<head> |
|
5 |
<meta charset="utf-8"/> |
|
6 |
<meta name="keywords" content="百度地图,百度地图API,百度地图自定义工具,百度地图所见即所得工具"/> |
|
7 |
<meta name="description" content="百度地图API自定义地图,帮助用户在可视化操作下生成百度地图"/> |
|
8 |
<title>百度地图API自定义地图</title> |
|
9 |
<!--引用百度地图API--> |
|
10 |
<style type="text/css"> |
|
11 |
html, body { |
|
12 |
margin: 0; |
|
13 |
padding: 0; |
|
14 |
overflow: hidden; |
|
15 |
} |
|
16 |
</style> |
|
17 |
<script type="text/javascript" src="http://api.map.baidu.com/api?key=&v=1.1&services=true"></script> |
|
18 |
</head> |
|
19 |
|
|
20 |
<body onload="initMap();"> |
|
21 |
<!--百度地图容器--> |
|
22 |
<div style="width:697px;height:550px;border:#ccc solid 1px;" id="dituContent"></div> |
|
23 |
</body> |
|
24 |
<script type="text/javascript"> |
|
25 |
function getParam(name) { |
|
26 |
return location.href.match(new RegExp('[?#&]' + name + '=([^?#&]+)', 'i')) ? RegExp.$1 : ''; |
|
27 |
} |
|
28 |
var map, marker; |
|
29 |
var centerParam = getParam('center'); |
|
30 |
var zoomParam = getParam('zoom'); |
|
31 |
var widthParam = getParam('width'); |
|
32 |
var heightParam = getParam('height'); |
|
33 |
var markersParam = getParam('markers'); |
|
34 |
var markerStylesParam = getParam('markerStyles'); |
|
35 |
|
|
36 |
//创建和初始化地图函数: |
|
37 |
function initMap() { |
|
38 |
// [FF]切换模式后报错 |
|
39 |
if (!window.BMap) { |
|
40 |
return; |
|
41 |
} |
|
42 |
var dituContent = document.getElementById('dituContent'); |
|
43 |
dituContent.style.width = widthParam + 'px'; |
|
44 |
dituContent.style.height = heightParam + 'px'; |
|
45 |
|
|
46 |
createMap();//创建地图 |
|
47 |
setMapEvent();//设置地图事件 |
|
48 |
addMapControl();//向地图添加控件 |
|
49 |
|
|
50 |
// 创建标注 |
|
51 |
var markersArr = markersParam.split(','); |
|
52 |
var point = new BMap.Point(markersArr[0], markersArr[1]); |
|
53 |
marker = new BMap.Marker(point); |
|
54 |
marker.enableDragging(); |
|
55 |
map.addOverlay(marker); // 将标注添加到地图中 |
|
56 |
|
|
57 |
if(parent.editor && parent.document.body.contentEditable=="true") { //在编辑状态下 |
|
58 |
setMapListener();//地图改变修改外层的iframe标签src属性 |
|
59 |
} |
|
60 |
} |
|
61 |
|
|
62 |
//创建地图函数: |
|
63 |
function createMap() { |
|
64 |
map = new BMap.Map("dituContent");//在百度地图容器中创建一个地图 |
|
65 |
var centerArr = centerParam.split(','); |
|
66 |
var point = new BMap.Point(parseFloat(centerArr[0]), parseFloat(centerArr[1]));//定义一个中心点坐标 |
|
67 |
map.centerAndZoom(point, parseInt(zoomParam));//设定地图的中心点和坐标并将地图显示在地图容器中 |
|
68 |
} |
|
69 |
|
|
70 |
//地图事件设置函数: |
|
71 |
function setMapEvent() { |
|
72 |
map.enableDragging();//启用地图拖拽事件,默认启用(可不写) |
|
73 |
map.enableScrollWheelZoom();//启用地图滚轮放大缩小 |
|
74 |
map.enableDoubleClickZoom();//启用鼠标双击放大,默认启用(可不写) |
|
75 |
map.enableKeyboard();//启用键盘上下左右键移动地图 |
|
76 |
} |
|
77 |
|
|
78 |
//地图控件添加函数: |
|
79 |
function addMapControl() { |
|
80 |
//向地图中添加缩放控件 |
|
81 |
var ctrl_nav = new BMap.NavigationControl({anchor: BMAP_ANCHOR_TOP_LEFT, type: BMAP_NAVIGATION_CONTROL_LARGE}); |
|
82 |
map.addControl(ctrl_nav); |
|
83 |
//向地图中添加缩略图控件 |
|
84 |
var ctrl_ove = new BMap.OverviewMapControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT, isOpen: 1}); |
|
85 |
map.addControl(ctrl_ove); |
|
86 |
//向地图中添加比例尺控件 |
|
87 |
var ctrl_sca = new BMap.ScaleControl({anchor: BMAP_ANCHOR_BOTTOM_LEFT}); |
|
88 |
map.addControl(ctrl_sca); |
|
89 |
} |
|
90 |
|
|
91 |
function setMapListener() { |
|
92 |
var editor = parent.editor, containerIframe, |
|
93 |
iframes = parent.document.getElementsByTagName('iframe'); |
|
94 |
for (var key in iframes) { |
|
95 |
if (iframes[key].contentWindow == window) { |
|
96 |
containerIframe = iframes[key]; |
|
97 |
break; |
|
98 |
} |
|
99 |
} |
|
100 |
if (containerIframe) { |
|
101 |
map.addEventListener('moveend', mapListenerHandler); |
|
102 |
map.addEventListener('zoomend', mapListenerHandler); |
|
103 |
marker.addEventListener('dragend', mapListenerHandler); |
|
104 |
} |
|
105 |
|
|
106 |
function mapListenerHandler() { |
|
107 |
var zoom = map.getZoom(), |
|
108 |
center = map.getCenter(), |
|
109 |
marker = window.marker.getPoint(); |
|
110 |
containerIframe.src = containerIframe.src. |
|
111 |
replace(new RegExp('([?#&])center=([^?#&]+)', 'i'), '$1center=' + center.lng + ',' + center.lat). |
|
112 |
replace(new RegExp('([?#&])markers=([^?#&]+)', 'i'), '$1markers=' + marker.lng + ',' + marker.lat). |
|
113 |
replace(new RegExp('([?#&])zoom=([^?#&]+)', 'i'), '$1zoom=' + zoom); |
|
114 |
editor.fireEvent('saveScene'); |
|
115 |
} |
|
116 |
} |
|
117 |
</script> |
|
118 |
</html> |