曹え 5811 发布于:2018-09-28 16:05:33
<div class="mapBox" id="allmap"></div> <!-- 百度地图 --> <script type="text/javascript" src="https://api.map.baidu.com/getscript?v=2.0&ak=iZBwFRYOBOBbZEftoKTk30LFsD6lqG3Z&services=&t=20180823194355"></script> <script> //创建和初始化地图函数: function initMap(){ createMap();//创建地图 setMapEvent();//设置地图事件 addMapOverlay();//向地图添加覆盖物 } function createMap(){ map = new BMap.Map("allmap"); map.centerAndZoom(new BMap.Point(118.935862,32.101301),16); } function setMapEvent(){ } function addClickHandler(target,window){ target.addEventListener("click",function(){ target.openInfoWindow(window); }); } function addMapOverlay(){ var markers = [ {content:"地址:南京市栖霞区东墅山庄",title:"希妍恋",imageOffset: {width:-46,height:-21},position:{lat:32.098854,lng:118.936114}} ]; for(var index = 0; index < markers.length; index++ ){ var point = new BMap.Point(markers[index].position.lng,markers[index].position.lat); var marker = new BMap.Marker(point,{icon:new BMap.Icon("http://api.map.baidu.com/lbsapi/createmap/images/icon.png",new BMap.Size(20,25),{ imageOffset: new BMap.Size(markers[index].imageOffset.width,markers[index].imageOffset.height) })}); // var label = new BMap.Label(markers[index].title,{offset: new BMap.Size(25,5)}); var opts = { width: 200, title: markers[index].title, enableMessage: false }; var infoWindow = new BMap.InfoWindow(markers[index].content,opts); // marker.setLabel(label); marker.addEventListener('click',map.openInfoWindow(infoWindow, point)); addClickHandler(marker,infoWindow); map.addOverlay(marker); }; } var map; initMap(); </script>
登录后可以留言提问!
微信扫码登录