var mapType;
var baidMap;
var idsData;
var markers =[];
var namekg = false;
var newdata;
var latlngArr =[];
function googleToBaidu(glng,glat) {


}
function cutoverMap() {
	var  myselect=document.getElementById("select");
	var index = myselect.selectedIndex ;
	mapType = myselect.options[index].value;
	latlngArr.length=0;
	close_sudu();
	console.info("----->"+mapType);
	if(1==mapType){
		document.getElementById("map_canvas").style.display="none";
		document.getElementById("container").style.display="block";
		if(baidMap==null ||baidMap==""){
			baiduMap(carid);
		}else{
			al();
		}
		var map_canvas = document.getElementById("container");
		var clientwidth= document.body.clientWidth;
		map_canvas.style.width = clientwidth+"px";
		baidMap.addEventListener('resize', function(){
			baidMap.setCenter(new BMap.Point(carList.data[0].positionLast.blng,carList.data[0].positionLast.blat));
		});

		var ctrl = new BMapLib.TrafficControl({
			showPanel: false //是否显示路况提示面板
		});
		baidMap.addControl(ctrl);
		ctrl.setAnchor(BMAP_ANCHOR_BOTTOM_RIGHT);

	}else if(3 == mapType){
		$("#map_canvas").empty();
		$("#map_canvas").show();
		$("#container").hide();
		loadGaodeMapJs();//加载高德地图js
		setTimeout('loadGaoDeTrack()',500);
	}else{
//		document.getElementById("container").style.display="none";
//		document.getElementById("map_canvas").style.display="block";
//		console.info("map:",map);
//		if(map==null){
//		console.info("1.");
		initialize();
//		}else{
//		console.info("2.");
//		al();
//		}
//		var map_canvas= document.getElementById("map_canvas");
//		var clientwidth= document.body.clientWidth;
//		map_canvas.style.width = clientwidth+"px";
//		google.maps.event.trigger(map, 'resize');
//		map.panTo(new google.maps.LatLng(carList.data[0].positionLast.lat,carList.data[0].positionLast.lng));
	}
	if(jiejingkg==true){
		close_jiejing();
	}
}

function baiduMap(ids) {
	//setT=setTimeout("MapData()",100);
	if(ids == ""){
		setTimeout("baiduMap(cartIds)",5000);
	}else if(ids == "-1"){
		baidMap = new BMap.Map("container");      // 创建地图实例    
		var point = new BMap.Point(116.404, 39.915);  // 创建点坐标
		baidMap.centerAndZoom(point, 15);
	}else{
		var lat,lng;
		for(i=0;i<carList.data.length;i++){
			if(carList.data[i].positionLast.lat != 0 &&carList.data[i].positionLast.lng != 0){
				lat = carList.data[i].positionLast.lat;
				lng = carList.data[i].positionLast.lng;
				break;
			}
		}

		idsData=ids;

		baidMap = new BMap.Map("container");      // 创建地图实例    
		var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
		//alert(x_pi);

		var x = lng, y = lat;
		var z =Math.sqrt(x * x + y * y) + 0.00002 *Math.sin(y * x_pi);
		var theta =Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
		lng = z *  Math.cos(theta) + 0.0065;
		lat = z *  Math.sin(theta) + 0.006;
		var point = new BMap.Point(lng,lat);

		//var point = new BMap.Point(lng,lat );  // 创建点坐标
		latlngArr.push(point);
		baidMap.centerAndZoom(point, 15);// 初始化地图，设置中心点坐标和地图级别
		baidMap.addControl(new BMap.NavigationControl());  //缩放轴


		baidMap.addControl(new BMap.OverviewMapControl());    //缩略图
		baidMap.addControl(new BMap.MapTypeControl({mapTypes: [BMAP_NORMAL_MAP,BMAP_HYBRID_MAP ]})); //地图类型

		baidMap.enableScrollWheelZoom(); //启用滚轮放大缩小
		baidMap.addControl(new BMap.ScaleControl());   // 添加默认比例尺控件

		var ctrl = new BMapLib.TrafficControl({
			showPanel: true //是否显示路况提示面板
		});      
		baidMap.addControl(ctrl);
		ctrl.setAnchor(BMAP_ANCHOR_TOP_RIGHT);
		ctrl.setOffset(new BMap.Size(90, 10))
		/*if(alSwitch == false){
  			countDown();    //每隔15秒执行函数al
  			alSwitch = true;
  		}*/



		//var myLatlng = new BMap.Point(carList.data[0].positionLast.blng,carList.data[0].positionLast.blat);


		var s=10;
		var condition=carList.data[0].positionLast.state2;
		var sp = parseInt(carList.data[0].positionLast.speed);
		if (condition != null) {
			if (condition.indexOf("不在线")>=0 || condition.indexOf("掉线")>=0)
				s = 2;
			else {
				if (condition.indexOf("速报警")>=0)
					s = 4;
				else if (condition.indexOf("报警")>=0)
					s = 3;
				else if (sp > 0.5)
					s = 0;
				else
					s = 1;
			}
			if (s != 4 && s != 3 && condition.indexOf("停车")>=0
					&& !condition.indexOf("不在线")>= 0)
			{
				if(condition.indexOf("停车超时")>=0){
					s = 5;	
				}else{
					s = 6;
				}
			}
		}else{
			if (sp > 0.5)
				s = 0;
			else
				s = 1;
		}

		var newtime = new Date().getTime();
		var ttime =carList.data[0].positionLast.recvtime.replace("-","/");
		ttime=ttime.replace("-","/");
		ttime=ttime.replace("T"," ");
		var oldtime = new Date(ttime).getTime();
		if(newtime-oldtime > 600000){
			s=2;
		}	
		var state =s;
		var pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/red_0.gif";
		if(state == 0) pngicon = "gpsonline/image/pic/"+carList.data[i].icon+"/green_";
		else if(state == 1) pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/red_";
		else if(state == 2) pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/gray_";
		else if(state == 3) pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/yellow_";
		else if(state == 4) pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/pink_";
		else if(state == 5) pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/stopalarm";
		else pngicon = "gpsonline/image/pic/"+carList.data[0].icon+"/red_";
		var direction="";
		var t = parseInt(carList.data[0].positionLast.direct);
		if(t>27 && t<=72){
			if(state == 5){
				var image=".gif";
			}else{
				var image="45.gif";
			}

			direction="东北";
		}else if(t>72 && t<=117){
			if(state == 5){
				var image=".gif";
			}else{
				var image="90.gif";
			}

			direction="东";
		}
		else if(t>117 && t<=162){
			if(state == 5){
				var image=".gif";
			}else{
				var image="135.gif";
			}
			direction="东南";
		}
		else if(t>162 && t<=207){
			if(state == 5){
				var image=".gif";
			}else{
				var image="180.gif";
			}
			direction="南";
		}
		else if(t>207 && t<=252){
			if(state == 5){
				var image=".gif";
			}else{
				var image="225.gif";
			}
			direction="西南";
		}
		else if(t>252 && t<=297){
			if(state == 5){
				var image=".gif";
			}else{
				var image="270.gif";
			}
			direction="西";
		}
		else if(t>297 && t<=342){
			if(state == 5){
				var image=".gif";
			}else{
				var image="315.gif";
			}
			direction="西北";
		}
		else {
			if(state == 5){
				var image=".gif";
			}else{
				var image="0.gif";
			}
			direction="北";
		}
		var html =	"<b>设备名称：</b>"+carList.data[0].vhcCode+"</br>"
		+"<b>接收时间：</b>"+carList.data[0].positionLast.recvtime+"</br>"
		+"<b>速度：</b>"+carList.data[0].positionLast.speed.toFixed(1)+"km/h </br>"
		+"<b>方向：</b>"+direction+"</br>"
		+"<b>经纬度：</b>"+carList.data[0].positionLast.lng+","+carList.data[0].positionLast.lat+"</br>"
		+"<b>状态：</b>"+carList.data[0].positionLast.state2+"</br>"
		+"<b>位置：</b>"+carList.data[0].positionLast.memo+"</br>";
		var icon_size=real(carList.data[0].icon);
		var myIcon = new BMap.Icon(pngicon+image, new BMap.Size(icon_size,icon_size));
		var marker = new BMap.Marker(
				point
				,{icon:myIcon}
		);  // 创建标注
		baidMap.addOverlay(marker);
		/*var opts = {
					  position : myLatlng,    // 指定文本标注所在的地理位置
					  offset   : new BMap.Size(10, -10)    //设置文本偏移量

					}
					var label = new BMap.Label(carList.data[0].vhcCode, opts);  // 创建文本标注对象
					label.setStyle({
						 color : "#000000",
						 backgroundColor : "#f5fcb8",
						 borderColor : "#000000",
						 fontSize : "12px",
						 height : "16px",
						 lineHeight : "16px",
						 fontFamily:""
					 });
					baidMap.addOverlay(label); */
		//创建信息窗口
		var infoWindow = new BMap.InfoWindow(html);
		marker.addEventListener("click", function(){this.openInfoWindow(infoWindow);});

		var markerManage = new Object();
		markerManage.id = carList.data[0].id;
		markerManage.vhcCode = carList.data[0].vhcCode;
		//markerManage.label = label;
		markerManage.marker = marker;
		markerManage.icon = carList.data[0].icon;
		markerManage.infowindow = infoWindow;
		markerManage.latlng = point;
		markers.push(markerManage);
		marker.openInfoWindow(infoWindow);
	}
}

function updateBaiduMap(){

	//var myLatlng = new BMap.Point(carList.data[0].positionLast.blng,carList.data[0].positionLast.blat);
	var x_pi = 3.14159265358979324 * 3000.0 / 180.0;
	//alert(x_pi);
	var x = carList.data[0].positionLast.lng, y = carList.data[0].positionLast.lat;
	var z =Math.sqrt(x * x + y * y) + 0.00002 *Math.sin(y * x_pi);
	var theta =Math.atan2(y, x) + 0.000003 * Math.cos(x * x_pi);
	var lng = z *  Math.cos(theta) + 0.0065;
	var lat = z *  Math.sin(theta) + 0.006;
	var myLatlng = new BMap.Point(lng,lat);
	latlngArr.push(myLatlng);
	var s=10;
	var condition=carList.data[0].positionLast.state2;
	var sp = parseInt(carList.data[0].positionLast.speed);
	if (condition != null){
		if (condition.indexOf("不在线")>=0 || condition.indexOf("掉线")>=0)
			s = 2;
		else {
			if (condition.indexOf("速报警")>=0)
				s = 4;
			else if (condition.indexOf("报警")>=0)
				s = 3;
			else if (sp > 0.5)
				s = 0;
			else
				s = 1;
		}
		if (s != 4 && s != 3 && condition.indexOf("停车")>=0
				&& !condition.indexOf("不在线")>= 0){
			if(condition.indexOf("停车超时")>=0){
				s = 5;	
			}else{
				s = 6;
			}
		}
	}else{
		if (sp > 0.5)
			s = 0;
		else
			s = 1;
	}
	var newtime = new Date().getTime();
	var ttime =carList.data[0].positionLast.recvtime.replace("-","/");
	ttime=ttime.replace("-","/");
	ttime=ttime.replace("T"," ");
	var oldtime = new Date(ttime).getTime();
	if(newtime-oldtime > 600000){
		s=2;
	}
	var state =s;
	var pngicon = "gpsonline/image/pic/"+markers[0].icon+"/red_0.gif";
	if(state == 0) pngicon = "gpsonline/image/pic/"+markers[0].icon+"/green_";
	else if(state == 1) pngicon = "gpsonline/image/pic/"+markers[0].icon+"/red_";
	else if(state == 2) pngicon = "gpsonline/image/pic/"+markers[0].icon+"/gray_";
	else if(state == 3) pngicon = "gpsonline/image/pic/"+markers[0].icon+"/yellow_";
	else if(state == 4) pngicon = "gpsonline/image/pic/"+markers[0].icon+"/pink_";
	else if(state == 5) pngicon = "gpsonline/image/pic/"+markers[0].icon+"/stopalarm";
	else pngicon = "gpsonline/image/pic/"+markers[0].icon+"/red_";
	var direction="";
	var t = parseInt(carList.data[0].positionLast.direct);
	if(t>27 && t<=72){
		if(state == 5){
			var image=".gif";
		}else{
			var image="45.gif";
		}
		direction="东北";
	}else if(t>72 && t<=117){
		if(state == 5){
			var image=".gif";
		}else{
			var image="90.gif";
		}

		direction="东";
	}
	else if(t>117 && t<=162){
		if(state == 5){
			var image=".gif";
		}else{
			var image="135.gif";
		}
		direction="东南";
	}
	else if(t>162 && t<=207){
		if(state == 5){
			var image=".gif";
		}else{
			var image="180.gif";
		}
		direction="南";
	}
	else if(t>207 && t<=252){
		if(state == 5){
			var image=".gif";
		}else{
			var image="225.gif";
		}
		direction="西南";
	}
	else if(t>252 && t<=297){
		if(state == 5){
			var image=".gif";
		}else{
			var image="270.gif";
		}
		direction="西";
	}
	else if(t>297 && t<=342){
		if(state == 5){
			var image=".gif";
		}else{
			var image="315.gif";
		}
		direction="西北";
	}
	else {
		if(state == 5){
			var image=".gif";
		}else{
			var image="0.gif";
		}
		direction="北";
	}

	var html = "<b>设备名称：</b>"+markers[0].vhcCode+"</br>"
	+"<b>接收时间：</b>"+carList.data[0].positionLast.recvtime+"</br>"
	+"<b>速度：</b>"+carList.data[0].positionLast.speed.toFixed(1)+"km/h </br>"
	+"<b>方向：</b>"+direction+"</br>"
	+"<b>经纬度：</b>"+carList.data[0].positionLast.lng+","+carList.data[0].positionLast.lat+"</br>"
	+"<b>状态：</b>"+carList.data[0].positionLast.state2+"</br>"
	+"<b>位置：</b>"+carList.data[0].positionLast.memo+"</br>";
	document.getElementById('position').innerHTML = "位置: " +carList.data[0].positionLast.memo;					

	markers[0].latlng=myLatlng;
	markers[0].marker.setPosition(myLatlng);

	var icon_size=real(markers[0].icon);
	markers[0].marker.setIcon(new BMap.Icon(pngicon+image, new BMap.Size(icon_size,icon_size)));
	markers[0].infowindow.setPosition(myLatlng);
	markers[0].infowindow.setContent(html);
	baidMap.setCenter(myLatlng);
	var col="#00CD00";
	if(carList.data[0].positionLast.speed.toFixed(1)>100){
		col="#FF34B3";
	}
	if(latlngArr[latlngArr.length-1] != latlngArr[latlngArr.length-2]){
		var polyline = new BMap.Polyline([
		                                  latlngArr[latlngArr.length-1],
		                                  latlngArr[latlngArr.length-2]
		                                            ], {strokeColor:col, strokeWeight:3, strokeOpacity:1});
		baidMap.addOverlay(polyline);

	}
	if(jiejingkg==true){
		streeViewObject.open(carList.data[0].positionLast.blat,carList.data[0].positionLast.blng,carList.data[0].positionLast.direct,3);
	}
}
function real(id){
	if(id == "0"){
		return 24;
	}
	if(id == "1"){
		return 24;
	}
	if(id == "2"){
		return 24;
	}
	if(id == "3"){
		return 24;
	}
	if(id == "4"){
		return 24;
	}
	if(id == "5"){
		return 24;
	}
	if(id == "6"){
		return 24;
	}
	if(id == "7"){
		return 24;
	}
	if(id == "8"){
		return 24;
	}
	if(id == "9"){
		return 32;
	}
	if(id == "10"){
		return 24;
	}
	if(id == "12"){
		return 29;
	}
};
