欢迎您访问程序员文章站本站旨在为大家提供分享程序员计算机编程知识!
您现在的位置是: 首页

百度地图使用

程序员文章站 2024-03-21 13:14:28
...
function BMapManage(divName){
	 this.map = null;
     this.DivName = divName;
     
     this.lushu=null;
     this.MapManage=MapManage;     
     this.bPoints=[];//原始地图点数据
 	 this.carMark=null;
}

var MapManage = new BMapManage('container');

BMapManage.prototype.onLoad = function(){
	this.map = new BMap.Map(this.DivName);
	var point = new BMap.Point(116.296098,40.080640);    // 创建点坐标
	this.map.centerAndZoom(point,12);                     // 初始化地图,设置中心点坐标和地图级别。
	
	this.map.addControl(new BMap.NavigationControl()); 
	//map.addControl(new BMap.ScaleControl()); 		  // 添加比例尺控件
	//map.addControl(new BMap.OverviewMapControl()); 
	//map.addControl(new BMap.MapTypeControl());    //地图类型 地图、卫星、三维
	this.map.enableScrollWheelZoom();                  // 启用滚轮放大缩小。
	this.map.enableKeyboard();                         // 启用键盘操作。  
	this.map.setCurrentCity("北京");          // 设置地图显示的城市 此项是必须设置的
}

BMapManage.prototype.Clear = function(){
	this.map.clearOverlays();
}

//实时点
BMapManage.prototype.RealLocationVehicle = function(rData){
	this.Clear();
    var myIcon = new BMap.Icon("http://localhost:8080/js/lib/img/car.png",new BMap.Size(52,26),{anchor : new BMap.Size(27, 13)});
	for (var i = 0; i < rData.length; i++) {
	    var point = new BMap.Point(rData[i].DX,rData[i].DY);
	  	var marker = new BMap.Marker(point,{icon:myIcon,title: rData[i].AUTO_BRAND_NO});
		this.map.addOverlay(marker);  
    } 
}

//历史线
BMapManage.prototype.getLineHis = function(rData,label,flag){
	
	var arrPois = [];
	for(var i = 0; i < rData.length; i++){
		var point = new BMap.Point(rData[i].DX,rData[i].DY); 
		arrPois.push(point);
	}
    var polyline = new BMap.Polyline(arrPois, {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
	this.map.addOverlay(polyline);
	if(flag){
	this.map.setViewport(arrPois);
	}
	
	var myIcon = new BMap.Icon("image/car_y.png",new BMap.Size(40,40),{anchor : new BMap.Size(27, 13)});
	
	this.lushu = new BMapLib.LuShu(this.map,arrPois,{
        defaultContent:label,
        autoView:true,//是否开启自动视野调整,如果开启那么路书在运动过程中会根据视野自动调整

        speed:7000,
        icon:myIcon,
       // enableRotation:true,//是否设置marker随着道路的走向进行旋转
			landmarkPois: [
			       {}
		]
	}); 
}
//车辆在历史线上播放
BMapManage.prototype.carStart = function(){
	
	this.lushu.start();

	
}
//车辆在历史线上停止
BMapManage.prototype.carStop = function(){
	
	this.lushu.stop();

	
}
//起点库房
BMapManage.prototype.getStartPoint = function (rData) {
    var myIcon = new BMap.Icon("../Map/Img/tj/gds.gif",new BMap.Size(45,42),{anchor : new BMap.Size(27, 27)});
	for (var i = 0; i < rData.length; i++) {
		var point = new BMap.Point(rData[i].DX,rData[i].DY);
	  	var marker = new BMap.Marker(point,{icon:myIcon,title: rData[i].WH_NAME});
		this.map.addOverlay(marker);  
    }
}
//终点库房
BMapManage.prototype.getendPoint = function(rData){
	var myIcon = new BMap.Icon("../Map/Img/tj/gds2.gif",new BMap.Size(45,42),{anchor : new BMap.Size(27, 27)});
	for (var i = 0; i < rData.length; i++) {
		var point = new BMap.Point(rData[i].DX,rData[i].DY);
	  	var marker = new BMap.Marker(point,{icon:myIcon,title: rData[i].WH_NAME});
		this.map.addOverlay(marker);  
    }
}
//显示出所有的库房
BMapManage.prototype.ShowWH=function(rData){

	this.Clear();
    var myIcon = "";
    var w = 0;
    var m = 0;
    var markers = [];
    var whIds = [];
    var whTypes = [];      
    var marker=null;
    var pt =null;    
    for (var i = 0; i < rData.length; i++) {
    	var whId = rData[i].WH_ID;
    	//1为智能库房,2为普通库,3密集柜,4周转柜
    	if(rData[i].TYPE_CODE=='1'){
    		//立体库房    		
			 myIcon = new BMap.Icon("../baiduMap/img/body_LK.png",new BMap.Size(130,42),{anchor : new BMap.Size(27, 27)});
		}else if(rData[i].TYPE_CODE=='2'){
			//普通库
		 	 myIcon = new BMap.Icon("../baiduMap/img/body_LK.png",new BMap.Size(110,50),{anchor : new BMap.Size(27, 27)});
		}else if(rData[i].TYPE_CODE=='3'){
			//密集柜
		 	 myIcon = new BMap.Icon("../baiduMap/img/body_mjg.png",new BMap.Size(120,60),{anchor : new BMap.Size(27, 27)});
		}else if(rData[i].TYPE_CODE=='4'){
			//周转柜
		 	 myIcon = new BMap.Icon("../baiduMap/img/body_zzg.png",new BMap.Size(120,60),{anchor : new BMap.Size(27, 27)});
		}
        pt = new BMap.Point(rData[i].LONGITUDE, rData[i].LATITUDE);
       	marker=new BMap.Marker(pt,{icon:myIcon,title:rData[i].WH_NAME,WH_ID: rData[i].WH_ID});
       	whIds.push(whId);
       	whTypes.push(rData[i].TYPE_CODE);
        markers.push(marker);
        
       (function(){
			var index = i;
	    	markers[i].addEventListener("click", function(marker1){	
				parent.showWH(whIds[index],whTypes[index],marker1.point.lat,marker1.point.lng);
			});
	    })();
       
    }
   this.map.markers=markers;
   var markerClusterer = new BMapLib.MarkerClusterer(this.map, {markers:markers});
   markerClusterer.setGridSize(100);//粘性更大
   markerClusterer.setMaxZoom(2); //设置第7层就不聚合了
   
}
//打开消息窗口
BMapManage.prototype.openInfoWindow=function(index,infos){
	
	var i=index;
	var sContent =
	"<h4 style='margin:0 0 5px 0;padding:0.2em 0'>*</h4>" + 
	"<img style='float:right;margin:4px' id='imgDemo' src='http://app.baidu.com/map/images/*.jpg' width='139' height='104' title='*'/>" + 
	"<p style='margin:0;line-height:1.5;font-size:13px;text-indent:2em'>*坐落在中国北京市中心,故宫的南侧,与*广场隔长安街相望,是清朝皇城的大门...</p>" + 
	"</div>";
	var infoWindow = new BMap.InfoWindow(sContent);  // 创建信息窗口对象
	
	this.map.markers[i].openInfoWindow(infoWindow);
	
}
//关闭消息窗口
BMapManage.prototype.closeInfoWindow=function(){
	this.map.closeInfoWindow();
}
//
BMapManage.prototype.ShowFKPostion = function (rData) {	
	this.Clear();
    var myIcon = "";
    var w = 0;
    var m = 0;
    var markers = [];
    var whIds = [];
	for (var i = 0; i < rData.length; i++) {
		var whId = rData[i].WH_ID;
		if(rData[i].LEVEL_CODE=='01'){
			 myIcon = new BMap.Icon("../Map/Img/marker01.png",new BMap.Size(90,42),{anchor : new BMap.Size(27, 27)});
		}else{
		 	 myIcon = new BMap.Icon("../Map/Img/tj/gds3.gif",new BMap.Size(110,42),{anchor : new BMap.Size(27, 27)});
		}
		var point = new BMap.Point(rData[i].DX,rData[i].DY);
	  	var marker = new BMap.Marker(point,{icon:myIcon,title: rData[i].WH_NAME});
	    this.map.centerAndZoom(point, 15);
		this.map.addOverlay(marker);  
		markers.push(marker);
		whIds.push(whId);
		(function(){
			var index = i;
	    	markers[i].addEventListener("click", function(){
				parent.showWH(whIds[index]);
			});
	    })();
		
        if(rData[i].FAULT_NUM>0){
        	this.map.addOverlay(this.CreateLabelForku("故障",new BMap.Point(rData[i].DX, rData[i].DY), 10, -20, 30));
        	}
        if(rData[i].ALARM_NUM>0){	
        	this.map.addOverlay(this.CreateLabelForkuyj("预警",new BMap.Point( rData[i].DX,rData[i].DY),10, 5, 30));
        }
    }
}


BMapManage.prototype.CreateLabelForku = function (Label,Point, ww, hh, width) {
    var tempLabel = new BMap.Label( "<div style='background-color:#ff9900;border:1px solid black'><font size=2 color=black>" + Label + "</font></div>",{offset:new BMap.Size(30, -30), position:Point});
    return tempLabel;
}
BMapManage.prototype.CreateLabelForkuyj = function (Label, Point,  ww, hh, width) {
    var tempLabel = new BMap.Label( "<div style='background-color:#A4AD38;border:1px solid black'><font size=2 color=black>" + Label + "</font></div>",{offset:new BMap.Size(30, -10), position:Point});
    return tempLabel;
}

function showLine(){  
    var start = {    
          latlng: new BMap.Point(117.230847,39.129208),    
          name: '中心立库'  
    }  
    var end = {  
         latlng: new BMap.Point(117.290847,39.239208),    
         name: '武清分库' 
    }   
    var opts = {    
         mode: BMAP_MODE_DRIVING,  
         region: '天津'    
    }  
    var routeSearch=new BMap.RouteSearch();  
    routeSearch.routeCall(start,end,opts);    
   
}  


//车辆在实时轨迹
BMapManage.prototype.dynamicline = function(rData){
	
	
	 
	 
	this.bPoints.push(new BMap.Point(rData.DX,rData.DY)); 
	
	var  len=this.bPoints.length;
	
	var newLinePoints=this.bPoints.slice(len-2,len);
	var markLinePoints=this.bPoints.slice(len-1,len);
	if(len>0){
		//最后俩点画线
		var polyline = new BMap.Polyline(newLinePoints, {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5});
		this.map.addOverlay(polyline);
		//创建标注对象并添加到地图
		
		
		
		
	}
	
	if (this.carMark==null) {
		//最后一点添加车辆mark
		var myIcon = new BMap.Icon("image/car_y.png",new BMap.Size(40,40),{anchor : new BMap.Size(27, 13)});
		this.carMark=new BMap.Marker(markLinePoints[0],{icon:myIcon});
		this.map.addOverlay(this.carMark);
		//删除上一个车辆mark
		//this.map.removeOverlay(newLinePoints[0]);
	}
	
	this.carMark.setPosition(markLinePoints[0]);
	
	
	//设置新的中心点和显示级别
	var view=this.map.getViewport(eval(this.bPoints));
	
	this.map.centerAndZoom(view.center,view.zoom);
}
//清除实施线路的数据
BMapManage.prototype.clearbPoints = function(){
	 this.bPoints=[];
	 this.carMark=null;
}
//增大地图一级
BMapManage.prototype.magnify=function(){
	this.map.zoomIn();
}
//缩小地图一级
BMapManage.prototype.reduce=function(){
	this.map.zoomOut();
}
//在地图上定位到某点
BMapManage.prototype.redirect=function(lon,lat){
	//map.clearOverlays();
	var new_point = new BMap.Point(lon,lat);
	var marker = new BMap.Marker(new_point);  // 创建标注
	this.map.addOverlay(marker);              // 将标注添加到地图中
	this.map.panTo(new_point);                //转到该点位置
}