<div class="spacemap" id="spacemap" style='height:400px'> </div>
//地图 var Lat = 211;//纬度 var Lng = 111;//经度 var address = '上海大厦'; var map = new BMap.Map("spacemap");//在百度地图容器中创建一个地图 var point = new BMap.Point(Lng,Lat);//定义一个中心点坐标 map.centerAndZoom(point,17); //标注点数组 var markerArr = [{ title: address, content: "", point: Lng+'|'+Lat, isOpen: 0, icon: { w: 30, h: 30, l: 0, t: 0, x: 6, lb: 5 } } ]; //创建marker function addMarker() { 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); } })() } } //创建InfoWindow function createInfoWindow(i) { var json = markerArr[i]; var iw = new BMap.InfoWindow("<b class='iw_poi_title'>" + json.title + "</b>"); return iw; } //创建一个Icon function createIcon(json) { var icon = new BMap.Icon("img/5.jpg", 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; } addMarker();
<script src="http://api.map.baidu.com/api?v=2.0&ak=xriz5LkyF7KH46rd5DOL9Nsu"></script>