百度地图添加marker并获取InfoWindow里面的内容,实现marker的增加、删除、编辑
2015-04-17 10:00
441 查看
<!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>");
h
a53f
tml.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>
<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>");
h
a53f
tml.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>
相关文章推荐
- 百度地图开发实现点击地图添加Marker并获取地址信息
- Zookeeper客户端基本操作java实现——创建连接、创建节点、添加修改节点内容、获取子节点、获取节点数据、删除节点
- 原生js实现增加(addclass),删除(removeclass),判断是否存在(hasclass),如果存在删除,如果不存在添加(toggleclass)和获取类名(getbyclass)的方法
- 写一个简单的程序实现把txt文件里面有规则的内容全部添加到数据库中
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除 之代码规范
- JTree右键菜单实现编辑、添加、删除节点
- [置顶] Qt读写Xml文件、QTreeWidget项实现可拖动、编辑、添加、删除等
- Android RecyclerView 获取数据实现添加,删除功能
- js实现前台动态添加文本框,后台获取文本框内容
- bootstrap table实现双击可编辑、添加、删除行功能
- 在制作简历,想用DIV实现添加各项履历内容,每点击一次按钮就增加一次怎么实现
- Jquery 实现动态添加table tr 和删除tr 以及checkbox的全选 和 获取添加TR删除TR后的数据
- java实现单链表(增加节点,删除节点,根据下标获取节点,获取链表大小,链表逆序)
- 编辑表格@tableView实现增加、删除、移动的操作
- 实现在DataGridView的编辑列里面选择列类型ColmnType时,多一种类型CalendarColumn从而增加时间控件
- Android 百度地图 SDK v3.0.0 (三) 添加覆盖物Marker与InfoWindow的使用
- 百度地图植入android应用系列教程三实现百度地图上的覆盖物Marker与InfoWindow的使用
- GridView 实现自定义分页、排序、查询、添加、编辑、多选删除、导出、突出显示等常见应用实现