百度地图BMap使用示例
引入百度map js<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=ak"/> 初始化百度地图 map = new BMap.Map("mapdiv"); map.centerAndZoom(new BMap.Point(108.768241, 34.408032), 5); map.enableScrollWheelZoom(true); map.addControl(new BMap.NavigationControl());// 添加平移缩放控件 map.addControl(new BMap.ScaleControl()); // 添加比例尺控件 map.addControl(new BMap.OverviewMapControl()); //添加缩略地图控件 var copyCtrl = new BMap.CopyrightControl({anchor: BMAP_ANCHOR_BOTTOM_RIGHT}); map.setMinZoom(5); map.setMaxZoom(11); copyCtrl.addCopyright({ id: 1, content: "<span style='color:Red;font-size:16px;margin-right:10px;font-weight:bold'></span>" }); map.addControl(copyCtrl);
//map风格 map.setMapStyle({style:'light'}); var point = new BMap.Point("经纬度"); var marker = new BMap.Marker(point); marker.setIcon(new BMap.Icon("icon-path", new BMap.Size(24, 32))); //label样式 var label = new BMap.Label("labcontent", {offset: new BMap.Size(25, -25)}); label.setStyle({ width: "120px;", color: "#fff", fontSize: "12px", height: "20px", lineHeight: "20px", fontFamily: "微软雅黑", background: "#676768", border: "1px solid '#ff8355'", borderRadius: "5px", textAlgin: "left", 'z-index': 999999 }); label.hide(); marker.setLabel(label); map.addOverlay(marker); marker.setAnimation(BMAP_ANIMATION_BOUNCE); var content = "";
//点击标注显示内容 addClickHandler(content, marker);
//鼠标悬浮显示 addMarkerOverHandler(marker);
function openInfo(content, e) {
var p = e.target;
var point = new BMap.Point(p.getPosition().lng, p.getPosition().lat);
var infoWindow = new BMap.InfoWindow(content);
map.openInfoWindow(infoWindow, point);
}
//点击弹窗显示
function addClickHandler(content, marker) {
marker.addEventListener("click", function (e) {
openInfo(content, e)
});
}
function addMarkerOverHandler(marker) {
marker.addEventListener("mouseover", function (e) {
marker.setTop(true);
if (marker.getLabel()) {
marker.getLabel().show();
}
});
marker.addEventListener("mouseout", function (e) {
marker.setTop(false);
if (marker.getLabel()) {
marker.getLabel().hide();
}
});
}