一:代码简介
本次主要将百度地图的经纬度转换,反地址解析,显示可视区域内的标注点,Markers聚合等整合到了同一个jsp页面上;旨在实现在经纬度未转换的情况下,页面自动实现百度经纬度转换,添加标注点。当用户点击标注点时,开始进行反地址解析,并检索该点周围区域内的最多3个关键点,一并予以显示。本次经纬度转换采取了百度的单个经纬度转换的方法,因为需要给标注点添加对应的信息,而百度提供的批量转换经纬度的方法会导致经纬度顺序错乱,无法使标注点与信息对应。同时,代码提供了地图页面自动刷新的方法,关于自动刷新,本人采用了setTimeout延时器,关于清除setTimeout延时器这块本人不是很精通,无法将延时器全部清除,所以此次采用的方法比较笨拙,还请大家见谅,望高手多多指点下.另外还有Markers标注点聚合,这个就比较简单了。此外,这次jsp页面共包含了两个地图容器,可以通过按钮切换。
附注:本人在地图上还添加了判断点击是地图还是覆盖物事件,如果点击到覆盖物,将无法拖动地图,需再次点击地图才可拖动地图移动。
二:功能预览
1.地图容器
(1).Markers聚合
(2).定位页面
2.反地址解析
3.显示可视区域内的标注点(通过监视地图移动、缩放开始,移动结束和地图缩放开始、结束来触发,2秒后执行方法)
4.地图自动刷新
5.Markers聚合
三、示例代码:
========================全部源码===========================================
<%@ page language="java" import="java.util.*" pageEncoding="utf-8"%> <html> <head> <Meta http-equiv="Content-type" content="text/html; charset=utf-8" /> <title>多车实时定位</title> <script type="text/javascript" src="http://api.map.baidu.com/api?v=1.3"></script> <script type="text/javascript" src="http://developer.baidu.com/map/jsdemo/demo/convertor.js"></script> <script type="text/javascript" src="http://api.map.baidu.com/library/GeoUtils/1.2/src/GeoUtils_min.js"></script> <script type="text/javascript" src="http://api.map.baidu.com/library/TextIconOverlay/1.2/src/TextIconOverlay_min.js"></script> <script type="text/javascript" src="http://api.map.baidu.com/library/MarkerClusterer/1.2/src/MarkerClusterer_min.js"></script> <script type="text/javascript" src="js/jquery-1.4.4.min.js"></script> <script src="http://ajax.googleapis.com/ajax/libs/jquery/1.7/jquery.min.js"></script> <style> .style1{ width: 400px; height: 120px; margin: 0px auto; margin-bottom:20px; border:1px solid #96C2F1; background-color: #EFF7FF } </style> <script type="text/javascript"> var t=""; var tt2=''; function end(){ tt2=setTimeout(addMymarkers,2000); } function cle(){ clearTimeout(tt2); } var t1='';var t2='';var t3='';var t4='';var t5='';var t6='';var t7='';var t8='';var t9='';var t10=''; var t11='';var t12='';var t13='';var t14='';var t15='';var t16='';var t17='';var t18='';var t19='';var t20=''; var t21=''; var JHt=''; function nextJH(){ JHt=setTimeout(function(){ refreshJH(); },20000); } var secs =120; //倒计时的秒数 function nextTrans2(){ var text1 = document.getElementById("text1").value; var text2= document.getElementById("text2").value; if(text1!=""||text2!=""){ X2.Eval("var pdnextTrans2 = 0"); document.getElementById("ShowDiv2").style.display="block"; //for(var i=secs;i>=0;i--){ // t=setTimeout('doUpdate(' + i + ')',(secs-i) * 1000); ///} t1=setTimeout('doUpdate('+20+')',0 * 1000); t2=setTimeout('doUpdate('+19+')',1 * 1000); t3=setTimeout('doUpdate('+18+')',2 * 1000); t4=setTimeout('doUpdate('+17+')',3 * 1000); t5=setTimeout('doUpdate('+16+')',4 * 1000); t6=setTimeout('doUpdate('+15+')',5 * 1000); t7=setTimeout('doUpdate('+14+')',6 * 1000); t8=setTimeout('doUpdate('+13+')',7 * 1000); t9=setTimeout('doUpdate('+12+')',8 * 1000); t10=setTimeout('doUpdate('+11+')',9 * 1000); t11=setTimeout('doUpdate('+10+')',10 * 1000); t12=setTimeout('doUpdate('+9+')',11 * 1000); t13=setTimeout('doUpdate('+8+')',12 * 1000); t14=setTimeout('doUpdate('+7+')',13 * 1000); t15=setTimeout('doUpdate('+6+')',14 * 1000); t16=setTimeout('doUpdate('+5+')',15 * 1000); t17=setTimeout('doUpdate('+4+')',16 * 1000); t18=setTimeout('doUpdate('+3+')',17 * 1000); t19=setTimeout('doUpdate('+2+')',18 * 1000); t20=setTimeout('doUpdate('+1+')',19 * 1000); t21=setTimeout('doUpdate('+0+')',20 * 1000); }else{ alert("对不起,您选中任何车辆,请选择相应车辆......"); } } function doUpdate(num){ document.getElementById('ShowDiv2').innerHTML = '<font color=red>'+num+'</font>秒后自动刷新<input type="button" value="停止" onclick="StopnextTrans2();"/><input type="button" value="车辆分布" onclick="JHmap()" /><input type="button" value="车辆追踪" onclick="Lmap()" />' ; if(num == 0) { post(); } } function StopnextTrans2(){ X2.Eval("var pdnextTrans2 = 1"); //for(var i=40;i>=0;i--){ // clearTimeout(t); //} clearTimeout(t1);clearTimeout(t2);clearTimeout(t3);clearTimeout(t4);clearTimeout(t5);clearTimeout(t6);clearTimeout(t7);clearTimeout(t8);clearTimeout(t9);clearTimeout(t10); clearTimeout(t11);clearTimeout(t12);clearTimeout(t13);clearTimeout(t14);clearTimeout(t15);clearTimeout(t16);clearTimeout(t17);clearTimeout(t18);clearTimeout(t19);clearTimeout(t20);clearTimeout(t21); document.getElementById('ShowDiv2').innerHTML = '<input type="button" value="开始自动定位" onclick="location2()" /><input type="button" value="车辆分布" onclick="JHmap()" /><input type="button" value="车辆追踪" onclick="Lmap()" />'; } function post() { var points=document.getElementById("text1").value; var infomation=document.getElementById("text2").value; if(points!="" && infomation!=""){ points = points.substring(0,points.length-1); var points2="'points':["+points+"]"; infomation = infomation.substring(0,infomation.length-1); var infomation2="'infomation':["+infomation+"]"; var dstr3="'is_gps' : 3"; var dszhong=" "+points2+","+infomation2+","+dstr3+" "; eval("var data3 = {"+ dszhong +"}"); map.clearOverlays(); for(i=0;i<data3.points.length;i++){ gpsxy(data3.points[i].lng,data3.points[i].lat,data3.infomation[i].vehiclelic,data3.infomation[i].terminal,data3.infomation[i].velocity,data3.infomation[i].direction,data3.infomation[i].datetime,data3.infomation[i].isonline,data3.infomation[i].height,data3.infomation[i].mileage,data3.is_gps); } if(pdnextTrans2==0){ nextTrans2(); } }else{ document.getElementById('ShowDiv2').innerHTML = '<input type="button" value="开始自动定位" onclick="location2()" /><input type="button" value="车辆分布" onclick="JHmap()" /><input type="button" value="车辆追踪" onclick="Lmap()" />'; alert("对不起,你所选的车辆从数据库中被删除或不存在,请您核实后再定位..."); } } function refreshJH() { var text1 = document.getElementById("text1").value; var text2= document.getElementById("text2").value; var dstr3 = document.getElementById("text3").value; if(text1!="" && text2!=""){ text1 = text1.substring(0,text1.length-1); var points2="'points':["+text1+"]"; text2 = text2.substring(0,text2.length-1); var infomation2="'infomation':["+text2+"]"; var dstr3="'is_gps' : 3"; var dszhong=" "+points2+","+dstr3+" "; eval("var data3 = {"+ dszhong +"}"); map2.clearOverlays(); juhe(data3); nextJH(); }else{ alert("对不起,你所选的车辆从数据库中被删除或不存在,请您核实后再定位..."); } } </script> <script type="text/javascript"> var X2 = {} X2.Eval = function(code) { if (!!(window.attachEvent && !window.opera)) { execScript(code); } else { window.eval(code); } } X2.Eval("var pdnextTrans2 = 1"); function addMymarkers(){ var text1 = document.getElementById("text1").value; var text2= document.getElementById("text2").value; var dstr3 = document.getElementById("text3").value; if(text1!=""||text2!=""){ var dstr = "'points':["; var lastIndex = text1.lastIndexOf(','); text1 = text1.substring(0,lastIndex); dstr += text1; dstr += "]"; var dstr2 = "'infomation':["; var lastIndex2 = text2.lastIndexOf(','); text2 = text2.substring(0,lastIndex2); dstr2 += text2; dstr2 += "]"; var dszhong = " " + dstr + "," + dstr2 + "," + dstr3 + " "; $("#datastr").text(dszhong); eval("var data3 = {" + dszhong + "}"); map.clearOverlays(); for(i=0;i<data3.points.length;i++){ gpsxy(data3.points[i].lng,data3.is_gps); } } } function addMarker22(str){ var addrall=""; var info=""; var addr = str.split("*"); for(var i=0;i<addr.length;i++){ var addr2=addr[i]; var addr3=addr2.split("."); var addr4=addr3[0]; addrall+=addr4; } document.getElementById('infoadds').innerHTML = addrall; } function ceshi() { var text1 = document.getElementById("text1").value; var text2= document.getElementById("text2").value; var dstr3 = document.getElementById("text3").value; if(text1!=""||text2!=""){ //#####开始拼装指定格式##### var dstr = "'points':["; //截取最后, var lastIndex = text1.lastIndexOf(',lastIndex); dstr += text1; dstr += "]"; //################### var dstr2 = "'infomation':["; var lastIndex2 = text2.lastIndexOf(',lastIndex2); dstr2 += text2; dstr2 += "]"; //#####结束拼装指定格式#### var dszhong = " " + dstr + "," + dstr3 + " "; eval("var data3 = {" + dszhong + "}"); map.clearOverlays(); for(i=0;i<data3.points.length;i++){ gpsxy(data3.points[i].lng,data3.is_gps); } } } function location2(){ var text1 = document.getElementById("text1").value; var text2= document.getElementById("text2").value; if(text1!=""||text2!=""){ nextTrans2(); }else{ alert("对不起,您未选中任何车辆,请选择相应车辆......"); //X2.Eval("var pdnextTrans2 = 1"); } } function JHmap(){ document.getElementById("JH_map").style.zIndex="0"; document.getElementById("load_map").style.zIndex="-1"; document.getElementById("ShowDiv").style.zIndex="0"; document.getElementById("ShowDiv2").style.zIndex="-1"; } function Lmap(){ document.getElementById("JH_map").style.zIndex="-1"; document.getElementById("load_map").style.zIndex="0"; document.getElementById("ShowDiv").style.zIndex="-1"; document.getElementById("ShowDiv2").style.zIndex="0"; } window.onload = refreshJH; </script> </head> <body> <div style="position: absolute;z-index: -5;"> <input type="text" name="text1" id="text1" value="{'lat':'36.68471','lng':'116.96866'},{'lat':'29.658484','lng':'91.121523'},{'lat':'43.817816','lng':'87.61647'},{'lat':'36.64095','lng':'101.757245'},{'lat':'30.682457','lng':'104.057625'},{'lat':'25.037929','lng':'102.699594'},{'lat':'45.76039','lng':'126.637684'},{'lat':'41.807471','lng':'123.458779'},{'lat':'23.109016','lng':'113.303839'},{'lat':'31.316628','lng':'120.606865'},{'lat':'37.119594','lng':'79.920327'}," /> <input type="text" name="text2" id="text2" value="{'vehiclelic':'鲁A*****','terminal':'****','velocity':'****','direction':'正北','datetime':'****','isonline':'0','height':'****','mileage':'****'},{'vehiclelic':'藏F*****','direction':'正南','isonline':'1',{'vehiclelic':'新A*****','direction':'正西',{'vehiclelic':'青H*****',{'vehiclelic':'川E*****','direction':'西北',{'vehiclelic':'云A*****',{'vehiclelic':'黑N*****',{'vehiclelic':'辽V*****','direction':'东南',{'vehiclelic':'粤A*****',{'vehiclelic':'苏L*****','direction':'东北',{'vehiclelic':'新R*****'," /> <input type="hidden" name="text3" id="text3" value="'is_gps' : 1" /> </div> <div style="position: absolute; z-index: -1; width: 100%; height: 100%; border: 0px solid gray; margin-right: 0px; bordercolor: ; margin-left: 0px; margin-top: 0px; margin-bottom: 0px;" id="load_map"> <div class="loading"> 地图加载中,请稍候... </div> </div> <div style="position: absolute; z-index: 0; width: 100%; height: 100%; border: 0px solid gray; margin-right: 0px; bordercolor: ; margin-left: 0px; margin-top: 0px; margin-bottom: 0px;" id="JH_map"> <div class="loading"> 地图加载中,请稍候... </div> </div> <div STYLE="position:absolute;z-index:0;" id="ShowDiv"> <input type="button" value="车辆分布" onclick="JHmap()" /> <input type="button" value="车辆追踪" onclick="Lmap()" /> </div> <div STYLE="position:absolute;z-index:-1;" id="ShowDiv2"> <input type="button" value="开始自动定位" onclick="location2()" /> <input type="button" value="车辆分布" onclick="JHmap()" /> <input type="button" value="车辆追踪" onclick="Lmap()" /> </div> <script> var map = new BMap.Map("load_map"); map.centerAndZoom(new BMap.Point(100.404,36.915),5); map.addControl(new BMap.MapTypeControl()); map.enableScrollWheelZoom(); map.enableKeyboard(); var ctrl_nav = new BMap.NavigationControl( { anchor : BMAP_ANCHOR_TOP_LEFT,type : BMAP_NAVIGATION_CONTROL_LARGE }); map.addControl(ctrl_nav); var ctrl_ove = new BMap.OverviewMapControl( { anchor : BMAP_ANCHOR_BOTTOM_RIGHT,isOpen : 1 }); map.addControl(ctrl_ove); var ctrl_sca = new BMap.ScaleControl( { anchor : BMAP_ANCHOR_BOTTOM_LEFT }); map.addControl(ctrl_sca); map.addControl(new BMap.NavigationControl()); var map2 = new BMap.Map("JH_map"); map2.centerAndZoom(new BMap.Point(100.404,5); map2.addControl(new BMap.MapTypeControl()); map2.enableScrollWheelZoom(); map2.enableKeyboard(); var ctrl_nav = new BMap.NavigationControl( { anchor : BMAP_ANCHOR_TOP_LEFT,type : BMAP_NAVIGATION_CONTROL_LARGE }); map2.addControl(ctrl_nav); var ctrl_ove = new BMap.OverviewMapControl( { anchor : BMAP_ANCHOR_BOTTOM_RIGHT,isOpen : 1 }); map2.addControl(ctrl_ove); var ctrl_sca = new BMap.ScaleControl( { anchor : BMAP_ANCHOR_BOTTOM_LEFT }); map2.addControl(ctrl_sca); map2.addControl(new BMap.NavigationControl()); function mapTrack() { var obj = this; this.getPoints = function() { $("body").append('<span id="datastr" style="display:none;"></span>'); ceshi(); } }; $(function() { var map = new mapTrack(); var trans_points = $("#trans_all_points"); if (trans_points.size() == 0) { map.getPoints(); } else { map.track(); } }) gpsxy = function (xx,yy,vehiclelic,terminal,velocity,direction,datetime,isonline,height,mileage,pd){ var info=""; if(isonline=='1'){ info="<div class='style1'><font color='red'>车牌号</font>:"+vehiclelic+"(在线)<font color='red'>终端SIM卡号</font>:"+terminal+"<font color='red'>速度</font>:"+velocity+"km/h<br/><font color='red'>行驶里程</font>:"+mileage+"km<font color='red'>方向</font>:"+direction+"<font color='red'>海拔高度</font>:"+height+"m<font color='red'>定位时间</font>:"+datetime+" <br/><font color='red'>地址</font>:<span id='infoadds'><img src='images/addrJDT.gif' /></span></div>"; //+addrall; }else if(isonline=='0'){ info="<div class='style1'><font color='red'>车牌号</font>:"+vehiclelic+"(离线)<font color='red'>终端SIM卡号</font>:"+terminal+"<font color='red'>速度</font>:"+velocity+"km/h<br/><font color='red'>行驶里程</font>:"+mileage+"km<font color='red'>方向</font>:"+direction+"<font color='red'>海拔高度</font>:"+height+"m<font color='red'>定位时间</font>:"+datetime+" <br/><font color='red'>地址</font>:<span id='infoadds'><img src='images/addrJDT.gif' /></span></div>"; //+addrall; } if(xx!=0.0||yy!=0.0){ var gpsPoint = new BMap.Point(xx,yy); BMap.Convertor.translate(gpsPoint,translateCallback = function (point){ var result = BMapLib.GeoUtils.isPointInRect(point,map.getBounds()); if(result == true){ var dire=direction; var dire2=""; if(isonline=='1'){ if(dire=='正东'){ dire2='images/onlineyou.png'; }else if(dire=='正西'){ dire2='images/onlinezuo.png'; }else if(dire=='正南'){ dire2='images/onlinexia.png'; }else if(dire=='正北'){ dire2='images/onlineshang.png'; }else if(dire=='东北'){ dire2='images/onlinedb.png'; }else if(dire=='西北'){ dire2='images/onlinexb.png'; }else if(dire=='东南'){ dire2='images/onlinedn.png'; }else if(dire=='西南'){ dire2='images/onlinexn.png'; } }else if(isonline=='0'){ if(dire=='正东'){ dire2='images/Nonlineyou.png'; }else if(dire=='正西'){ dire2='images/Nonlinezuo.png'; }else if(dire=='正南'){ dire2='images/Nonlinexia.png'; }else if(dire=='正北'){ dire2='images/Nonlineshang.png'; }else if(dire=='东北'){ dire2='images/Nonlinedb.png'; }else if(dire=='西北'){ dire2='images/Nonlinexb.png'; }else if(dire=='东南'){ dire2='images/Nonlinedn.png'; }else if(dire=='西南'){ dire2='images/Nonlinexn.png'; } } var myIcon = new BMap.Icon(dire2,new BMap.Size(50,50),{ offset: new BMap.Size(50,imageOffset: new BMap.Size(0,0) }); var marker = new BMap.Marker(point,{icon: myIcon}); var opts = { // 信息窗口宽度 height: 100,// 信息窗口高度 title : "<font size='4' color='#000000'><strong>车辆信息</strong></font>" } if(pd==0){ map.panTo(point); map.setZoom(15); } map.addOverlay(marker); var infoWindow = new BMap.InfoWindow(info,opts); // 创建信息窗口对象 infoWindow.setWidth(430); infoWindow.setHeight(130); marker.addEventListener("click",function(){ this.openInfoWindow(infoWindow); addallM2(point.lng,point.lat); }); //map.setCenter(point); //设置地图中心点 // marker.setAnimation(BMAP_ANIMATION_BOUNCE); //跳动的动画 var label = new BMap.Label(vehiclelic,{offset:new BMap.Size(20,-10)}); label.setStyle({color:'green',borderColor:'black'}); marker.setLabel(label); } } ); } } function addallM2(pt2,pt21){ var gc = new BMap.Geocoder(); var pt = new BMap.Point(pt2,pt21); var str = ""; var str2 = ""; var str3 = ""; gc.getLocation(pt,function(rs) { var addComp = rs.addressComponents; str2=addComp.province + "," + addComp.city + "," + addComp.district + "," + addComp.street + "," + addComp.streetNumber+""; var allPois = rs.surroundingPois; if (allPois.length != 0) { for (i = 0; i < allPois.length; ++i) { var juli = map.getDistance(pt,allPois[i].point); str += (i + 1) + "、离" + allPois[i].title + "("+ allPois[i].address + ")大约" + juli+ "*米 "; str3 = str2 + str; addMarker22(str3); } } else { gc.getLocation(pt," + addComp.streetNumber+""; var allPois = rs.surroundingPois; for (i = 0; i < allPois.length; ++i) { var juli = map.getDistance(pt,allPois[i].point); str += (i + 1) + "、离" + allPois[i].title + "("+ allPois[i].address + ")大约" + juli+ "*米 "; str3 = str2 + str; addMarker22(str3); } },mOption22); } },mOption21); } },mOption2); } function juhe(data){ var markers = []; for(i=0;i<data.infomation.length;i++){ var point = new BMap.Point(data.points[i].lng,data.points[i].lat); var dire=data.infomation[i].direction; var dire2=""; if(data.infomation[i].isonline=='1'){ if(dire=='正东'){ dire2='images/onlineyou.png'; }else if(dire=='正西'){ dire2='images/onlinezuo.png'; }else if(dire=='正南'){ dire2='images/onlinexia.png'; }else if(dire=='正北'){ dire2='images/onlineshang.png'; }else if(dire=='东北'){ dire2='images/onlinedb.png'; }else if(dire=='西北'){ dire2='images/onlinexb.png'; }else if(dire=='东南'){ dire2='images/onlinedn.png'; }else if(dire=='西南'){ dire2='images/onlinexn.png'; } }else if(data.infomation[i].isonline=='0'){ if(dire=='正东'){ dire2='images/Nonlineyou.png'; }else if(dire=='正西'){ dire2='images/Nonlinezuo.png'; }else if(dire=='正南'){ dire2='images/Nonlinexia.png'; }else if(dire=='正北'){ dire2='images/Nonlineshang.png'; }else if(dire=='东北'){ dire2='images/Nonlinedb.png'; }else if(dire=='西北'){ dire2='images/Nonlinexb.png'; }else if(dire=='东南'){ dire2='images/Nonlinedn.png'; }else if(dire=='西南'){ dire2='images/Nonlinexn.png'; } } var myIcon = new BMap.Icon(dire2,0) }); var marker2 = new BMap.Marker(point,{icon: myIcon}); markers.push(marker2); } var markerClusterer = new BMapLib.MarkerClusterer(map2,{markers:markers}); //markerClusterer.hide(); } map.addEventListener("zoomend",end); map.addEventListener("moveend",end); map.addEventListener("movestart",cle); map.addEventListener("zoomstart",cle); map.addEventListener("click",function(e){ if(e.overlay){ map.disableDragging(); }else{ map.enableDragging(); } }); var mOption2 = { poiRadius : 500,numPois : 3 } var mOption21 = { poiRadius : 3000,numPois : 3 } var mOption22 = { poiRadius : 6000,numPois : 3 } </script> </body> </html>
==============================================全部源码==========================================
四.总结:
通过此次学习,本人发现百度地图转换经纬度很是慢,不管你采用单个转换还是批量转换。另外百度的Markers聚合也没有本人想象中的好用,虽然百度说如果超过150个标注点建议用Markers聚合,但页面依然不是很流畅,不过用谷歌浏览器效果会稍微好点。
以上就是本人本次的学习总结,写的不是很好,还请各路高手,大侠们多多指点,嘿嘿
原文链接:/javaschema/286521.html