码迷,mamicode.com
首页 > 其他好文 > 详细

百度地图demo

时间:2016-08-08 06:22:33      阅读:191      评论:0      收藏:0      [点我收藏+]

标签:

以下代码拷贝成html,直接运行即能看到百度地图

<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8">
<title>百度地图</title>
</head>
<body>
<script type="text/javascript" src="http://api.map.baidu.com/api?key=&amp;v=1.1&amp;services=true"></script>
<div class="map-div" id="dituContent" style="height:600px">
<script type="text/javascript">
function initMap(){
createMap();
setMapEvent();
addMapControl();
addMarker();
}
function createMap(){
debugger;
var map = new BMap.Map("dituContent");
var point = new BMap.Point(121.517038,31.242691);
map.centerAndZoom(point,17);
window.map = map;
}

function setMapEvent(){
debugger;
map.enableDragging();
map.enableScrollWheelZoom();
map.enableDoubleClickZoom();
map.enableKeyboard();
}

function addMapControl(){
debugger;
var ctrl_nav = new BMap.NavigationControl({anchor:BMAP_ANCHOR_TOP_LEFT,type:BMAP_NAVIGATION_CONTROL_LARGE});
map.addControl(ctrl_nav);
var ctrl_ove = new BMap.OverviewMapControl({anchor:BMAP_ANCHOR_BOTTOM_RIGHT,isOpen:1});
map.addControl(ctrl_ove);
var ctrl_sca = new BMap.ScaleControl({anchor:BMAP_ANCHOR_BOTTOM_LEFT});
map.addControl(ctrl_sca);
}

var markerArr = [{title:"金钥匙集团",content:"上海陆家嘴浦东南路500号国家开发银行13楼<br />电话:400 052 6606",point:"121.517038|31.242691",isOpen:0,icon:{w:21,h:21,l:0,t:0,x:6,lb:5}}
];
function addMarker(){
debugger;
for(var i=0;i<markerArr.length;i++){
var json = markerArr[i];
var p0 = json.point.split("|")[0];
var p1 = json.point.split("|")[1];
var point = new BMap.Point(p0,p1);
var iconImg = createIcon(json.icon);
var marker = new BMap.Marker(point,{icon:iconImg});
var iw = createInfoWindow(i);
var label = new BMap.Label(json.title,{"offset":new BMap.Size(json.icon.lb-json.icon.x+10,-20)});
marker.setLabel(label);
map.addOverlay(marker);
label.setStyle({
borderColor:"#808080",
color:"#333",
cursor:"pointer"
});

(function(){
var index = i;
var _iw = createInfoWindow(i);
var _marker = marker;
_marker.addEventListener("click",function(){
this.openInfoWindow(_iw);
});
_iw.addEventListener("open",function(){
_marker.getLabel().hide();
})
_iw.addEventListener("close",function(){
_marker.getLabel().show();
})
label.addEventListener("click",function(){
_marker.openInfoWindow(_iw);
})
if(!!json.isOpen){
label.hide();
_marker.openInfoWindow(_iw);
}
})()
}
}
function createInfoWindow(i){
debugger;
var json = markerArr[i];
var iw = new BMap.InfoWindow("<b class=‘iw_poi_title‘ title=‘" + json.title + "‘>" + json.title + "</b><div class=‘iw_poi_content‘>"+json.content+"</div>");
return iw;
}
function createIcon(json){
debugger;
var icon = new BMap.Icon("http://app.baidu.com/map/images/us_mk_icon.png", new BMap.Size(json.w,json.h),{imageOffset: new BMap.Size(-json.l,-json.t),infoWindowOffset:new BMap.Size(json.lb+5,1),offset:new BMap.Size(json.x,json.h)})
return icon;
}
initMap();
</script>
</div>
</body>
</html>

百度地图demo

标签:

原文地址:http://www.cnblogs.com/cyh2009/p/5747816.html

(0)
(0)
   
举报
评论 一句话评论(0
登录后才能评论!
© 2014 mamicode.com 版权所有  联系我们:gaon5@hotmail.com
迷上了代码!