$(function(){ $(".baidumap").each(function(index){ var id = "baidumap" + index; $(this).attr("id", id); //自定义参数 var containerid = id; var coordinate = $(this).data("coordinate"); var content = $(this).html(); var type = $(this).data("type"); makemap(containerid, coordinate, content, type); }); }); function makemap(containerid, coordinate, content, type){ //获取经纬度 var latitude = coordinate.split(',')[0]; //经度 var longitude = coordinate.split(',')[1]; //纬度 // 百度地图api功能 var map = new bmap.map(containerid); var point = new bmap.point(latitude,longitude); //定位经纬度 map.centerandzoom(point, 15); // 初始化地图,设置中心点坐标和地图级别 map.enablescrollwheelzoom(true); //开启鼠标滚轮缩放 var marker = new bmap.marker(point); // 创建标注图标 map.addoverlay(marker); // 将标注添加到地图中 switch (type) { case "normal": var infowindow = new bmap.infowindow(content); marker.openinfowindow(infowindow); marker.addeventlistener('click',function(){ marker.openinfowindow(infowindow); }); break; case "detail": map.addcontrol(new bmap.navigationcontrol()); //pc端默认位于地图左上方,它包含控制地图的平移和缩放的功能。移动端提供缩放控件,默认位于地图右下方 map.addcontrol(new bmap.scalecontrol()); //默认位于地图左下方,显示地图的比例关系 map.addcontrol(new bmap.maptypecontrol()); //默认位于地图右上方,显示地图类型切换控件 map.addcontrol(new bmap.overviewmapcontrol()); //默认位于地图右下方,是一个可折叠的缩略地图 var opts = {width:0,height:0}; var infowindow = new bmap.infowindow(content, opts); marker.openinfowindow(infowindow); marker.addeventlistener('click',function(){ marker.openinfowindow(infowindow); }); break; case "simple": default: var label = new bmap.label(content,{offset:new bmap.size(30,0)}); //创建标注文字 marker.setlabel(label); // 显示标注文字 break; } }