wrap.html 2.18 KB
<link href="/pages/control/line/css/lineControl.css" rel="stylesheet" type="text/css" />
<div id="tab_map" class="portlet light portlet-fullscreen" style="transition: all .5s ease;padding: 0;" oncontextmenu=self.event.returnValue=false>
</div>
<script>
!function(){
	//加载地图页面
	$('#tab_map').load('/pages/mapmonitor/real/real.html', function(){
		$('#mapContainer').height('100%').css('margin-top', 0);
		$('#openWindow').remove();
		$('.leftUtils').css('width', '189px');
		
		startRefreshGpsTimer();
	});
	
	moment.locale('zh-cn');
	var storage = window.localStorage;
	var lineCodes = '';
	var allGps = {};
	//初始化lineCodes
	$.each(JSON.parse(storage.getItem('lineControlItems')), function(i, obj){
		lineCodes += (obj.lineCode + ',');
	});
	lineCodes = lineCodes.substr(0, lineCodes.length - 1);
	
	//为地图提供gps数据
	var gpsTimer;
	var realGpsT = 1000 * 10;
	function startRefreshGpsTimer(){
		var f = arguments.callee;
		refreshGpsProxy();
		gpsTimer = setTimeout(f, realGpsT);
	}
	
	function refreshGpsProxy(){
		refreshGps(function(add, up){
			$('#tab_map #mapContainer').trigger('gps_refresh', [add, up]);
		});
	}
	function refreshGps(cb){
		$.ajax({
			url: '/gps/real/line',
			data: {lineCodes: lineCodes},
			timeout: 5000,//5秒超时
			success: getGpsSuccess,
			error: getGpsError
		});
		
		function getGpsSuccess(gpsList){
			if(!gpsList || gpsList.length == 0)
				return;
			
			var prve = allGps
				,addArray = []
				,upArray = []
				,oldGps;
			for(var i = 0, gps; gps=gpsList[i++];){
				oldGps = prve[gps.deviceId];
				if(!oldGps){
					addArray.push(gps);
				}
				else if(gps.timestamp > oldGps.timestamp){
					//更新
					upArray.push(gps);
				}
				allGps[gps.deviceId] = gps;
			}
			cb && cb(addArray, upArray);
		}
		
		function getGpsError(jqXHR, textStatus){
			if(textStatus === 'error'){
				console.log(jqXHR, textStatus);
				layer.alert('获取GPS数据时出现异常,请尝试刷新页面!', {icon: 2});
			}
			else if(textStatus === 'timeout')
				layer.alert('连接服务器超时', {icon: 2});
			
			//停止gps刷新
			clearTimeout(gpsTimer);
		}
	}
}();
</script>