百度地图添加marker并获取InfoWindow里面的内容,实现marker的增加、删除、编辑

来源:互联网 发布:淘宝上架如何抢购 编辑:程序博客网 时间:2024/04/30 20:49
<!DOCTYPE html>
<html>
<head>
<meta http-equiv="Content-Type" content="text/html; charset=utf-8" />
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<style type="text/css">
body, html,#allmap {width: 100%;height: 100%;overflow: hidden;margin:0;font-family:"微软雅黑";}
</style>
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=FB14a133b9bf40fec90d36c84fb4dd13"></script>
<title>地图展示</title>
</head>
<body>
<div id="allmap" style="height:80%"></div>
<input type="button" id="btn" name="btn" value="加点" onclick="addPoint()" />
<input type="button" id="btn" name="btn" value="移除事件" onclick="removeEvent()" />
</body>
</html>
<script type="text/javascript">
// 百度地图API功能
var map = new BMap.Map("allmap",{enableMapClick: false});    // 创建Map实例
map.centerAndZoom(new BMap.Point(116.404, 39.915), 16);  // 初始化地图,设置中心点坐标和地图级别
map.addControl(new BMap.MapTypeControl());   //添加地图类型控件
map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
map.enableScrollWheelZoom(true);     //开启鼠标滚轮缩放


function savePoint(){//鼠标示例  鼠标点击拾取坐标点
alert(document.getElementById("test").value);
infoWindow.close();
infoWindow=[];
}
function del(arg){//点击时 地图上添加的点错误时删除
for(var i=0;i<arr.length;i++){
if(arr[i].k==arg){
map.removeOverlay(arr[i].marker);
break;
}
}
}
function addPoint(){//往地图上加点
map.addEventListener('click', clickEvent);
}
function removeEvent(){//移除click事件 
map.removeEventListener('click', clickEvent);
infoWindow.close();
infoWindowEdit.close();
}
var k=0;//用来区别marker
var infoWindow; // 将消息框添加到地图中
var infoWindowEdit;
var arr=new Array();//存放添加的点  方便删除
var clickEvent=function(e){
var lat=e.point.lat;
var lng=e.point.lng;
var point  = new BMap.Point(lng, lat);
var marker = new BMap.Marker(point);  // 创建标注
console.log("old  lat"+lat+"--"+lng);
map.addOverlay(marker);
arr.push({"lat":lat,"lng":lng,"point":point,"marker":marker,"k":++k});      
       var html = [];
       html.push("<div class=\"detailCarTip\" style='width:350px;height:100px'  >");
       html.push("<div style='font-size:14px;font-weight:bold;width:350px;'>" + "(" + "车辆信息title" + ")</div>");  //车辆信息
       html.push("<div style='border-bottom:1px solid #ff0000;style='width:350px;height:100px'></div>");     // 红线
       html.push("<table cellpadding=\"0\" cellspacing=\"0\" style='width:350px;height:100px' ");
       html.push("<tr>");
       html.push("<td><label>"+ "位置:" +"</label><input type='text' id='test' name='test' /></td>");
       html.push("<td><input type='button' id='test' name='test' value='保存' onclick='savePoint()' /></td><td><input type='button' id='test' name='test' value='删除' onclick=\'del("+k+")\' /></td></td><td><input type='button' id='test' name='test' value='完成' onclick='removeEvent()' /></td>"
        );
       html.push("</tr>");
       html.push("</table>");
       html.push("</div>");
       infoWindow = new BMap.InfoWindow(html.join(""), {enableMessage: false,enableCloseOnClick:false});  // 创建信息窗口对象
       marker.openInfoWindow(infoWindow); //开启信息窗口




       marker.addEventListener("click", function (e) { //点击时才打开消息框 
        var newLat=lat;
        var newLng=lng;
        var m;
        for(var i=0;i<arr.length;i++){
        if(arr[i].lat==newLat && arr[i].lng==newLng){
        m=arr[i].k;
        }
        }
        var val=newLat+"---"+newLng;
       var html = [];
       html.push("<div class=\"detailCarTip\" style='width:350px;height:100px'  >");
       html.push("<div style='font-size:14px;font-weight:bold;width:350px;'>" + "(" + "车辆信息title" + ")</div>");  //车辆信息
       html.push("<div style='border-bottom:1px solid #ff0000;style='width:350px;height:100px'></div>");     // 红线
       html.push("<table cellpadding=\"0\" cellspacing=\"0\" style='width:350px;height:100px' ");
       html.push("<tr>");
       html.push("<td><label>"+ "位置:" +"</label><input type='text' id='test' name='test' value="+"\""+val+"\""+"/></td>");//判断是否同一个marker
       html.push("<td><input type='button' id='test' name='test' value='保存' onclick='savePoint()' /></td><td><input type='button' id='test' name='test' value='删除' onclick=\'del("+m+")\' /></td></td><td><input type='button' id='test' name='test' value='完成' onclick='removeEvent()' /></td>"
        );
       html.push("</tr>");
       html.push("</table>");
       html.push("</div>");
       infoWindowEdit = new BMap.InfoWindow(html.join(""), {enableMessage: false,enableCloseOnClick:false});  // 创建信息窗口对象
        marker.openInfoWindow(infoWindowEdit); //开启信息窗口
       });

}

//新增方法  先创建 infow 这样才能在调用map.openInfoWindow(infoWindow,point);时  默认添加图标就是打开info窗口的

/*

 function ceateInfo(map,marker,address,mft_linkman,mft_mobile,point){
var infoWindow=[];
var html = [];
        html.push("<div style='width:250px;height:80px'  >");
        html.push("<div style='font-size:14px;font-weight:bold;width:250px;'>自提地址信息</div>");
        html.push("<div style='border-bottom:1px solid #ff0000;style='width:250px;height:80px'></div>");     // 红线
        html.push("<table cellpadding=\"0\" cellspacing=\"0\" style='width:250px;height:80px' ");
        html.push("<tr>");
        html.push("<td>自提联系人:" + mft_linkman +"</td>");
        html.push("</tr>");
        html.push("<tr>");
        html.push("<td>自提联电话:" + mft_mobile +"</td>");
        html.push("</tr>");
        html.push("<tr>");
        html.push("<td colspan='2'>自提联地址:" + address + "</td>");
        html.push("</tr>");
        html.push("</table>");
        html.push("</div>");
        infoWindow = new BMap.InfoWindow(html.join(""), {enableMessage: false});  // 创建信息窗口对象
marker.addEventListener("click", function () {
       marker.openInfoWindow(infoWindow); //开启信息窗口
   });
map.openInfoWindow(infoWindow,point);
 }

*/

</script>

0 0
原创粉丝点击