section-operation.js 6.48 KB
var SectionOperation = function () {
	var map, type = 0, editSection;
	var operation = {
		initMap: function() {
			var CENTER_POINT = {lng: 121.528733,lat: 31.237425};
			map = new BMap.Map('map_section');
			map.centerAndZoom(new BMap.Point(CENTER_POINT.lng, CENTER_POINT.lat), 15);
			map.enableDragging();
			map.enableScrollWheelZoom();
			map.disableDoubleClickZoom();
			map.enableKeyboard();
		},
		initSection: function() {
			operation.initMap();
			type = 0;
			var sectionId = $.url().param('no');
			SectionService.getSectionById(sectionId,function(section) {
				if (section) {
					operation.drawSection(section);
				}
			});
		},
		initSections: function () {
			operation.initMap();
			type = 1;
			var sectionName = $.url().param('no');
			if (sectionName) {
				SectionService.findSectionByName(sectionName, function (sections) {
					sections.forEach(function(section) {
						operation.drawSection(section);
					})
				})
			}
		},
		initEventBind: function () {
			$('#backUp').on('click',function() {
				loadPage('/pages/base/section/list.html');
			});
		},
		setSectionFormValue : function(section) {
			$('#edit_section_form input,select,textarea').each(function(){
				if (this.name) {
					$(this).val(section[this.name]);
				}
			})
		},
		recoverPolylineColor: function() {
			var overlays = map.getOverlays();
			overlays.forEach(function (overlay) {
				if (overlay.cdata && overlay.cdata.id) {
					overlay.setStrokeColor('#5298ff');
				}
			});
		},
		refreshPolylineData: function () {
			if (!type) {
				operation.initSection();
			} else {
				operation.initSections();
			}
		},
		drawSection : function(section) {
			var bsectionVectorWkt = section.bsectionVectorWkt;
			bsectionVectorWkt = bsectionVectorWkt.substring(11, bsectionVectorWkt.length - 1);
			var coordinates = bsectionVectorWkt.split(',');
			var centerPoint, points = new Array();
			var clen = coordinates.length, middle = Math.ceil(clen / 2);
			for(var i = 0;i < clen;i++) {
				var coordinate = coordinates[i].split(' ');
				points.push(new BMap.Point(coordinate[0], coordinate[1]));
				if (i == middle) {
					centerPoint = new BMap.Point(coordinate[0], coordinate[1])
				}
			}
			var polyline = new BMap.Polyline(points, {strokeColor : "#5298ff",strokeWeight : 6,strokeOpacity :1,strokeStyle:'solid'});
			polyline.cdata = section;
			map.addOverlay(polyline);
			var opts = {
				width: 200,
				height: 350,
				offset: new BMap.Size(0, 0),
				title: '<h4 style="color:#FFFFFF">' + (section.sectionName ? section.sectionName : '') + ' 路段详情</h4>',
				enableMessage: false,
				enableCloseOnClick: true,
				enableAutoPan: true
			};
			var html = new Array();
			html.push('<HR style="border:1 dashed #987cb9" width="100%" color=#987cb9 SIZE=1>');
			html.push('<span style="color:#DDD;font-size: 15px;">路段名称:');html.push(section.sectionName);html.push('</span>');
			html.push('<span class="help-block" style="color:#DDD;font-size: 15px;">路段编码:');html.push(section.sectionCode);html.push('</span>');
			html.push('<span class="help-block" style="color:#DDD;font-size: 15px;">交叉路段:');html.push(section.crosesRoad);html.push('</span>');
			html.push('<span class="help-block" style="color:#DDD;font-size: 15px;">东西向:');html.push((section.ewDirection === undefined ? '' : section.ewDirection == 0 ? '东->西' : '西->东'));html.push('</span>');
			html.push('<span class="help-block" style="color:#DDD;font-size: 15px;">南北向:');html.push((section.snDirection === undefined ? '' : section.snDirection == 0 ? '南->北' : '北->南'));html.push('</span>');
			html.push('<span class="help-block" style="color:#DDD;font-size: 15px;">路段限速:');html.push(section.speedLimit);html.push('</span>');
			html.push('<span class="help-block" style="color:#DDD;font-size: 15px;">路段长度:');html.push(section.sectionDistance);html.push('</span>');
			html.push('<button id="editSectionInfo" class="info_win_btn">修改路段信息</button>');
			html.push('<button id="movePolyline" class="info_win_btn">移动路段位置</button>');

			var infoWindow = new BMap.InfoWindow(html.join(''), opts);
			var icon = new BMap.Icon('/pages/base/stationroute/css/img/blank.gif', new BMap.Size(20, 20));
			var marker = new BMap.Marker(centerPoint, {icon: icon});
			map.addOverlay(marker);
			var panOpts = {noAnimation: true};
			map.panTo(centerPoint, panOpts);
			map.panBy(0, -110, panOpts);
			polyline.addEventListener('click', function(event) {
				operation.recoverPolylineColor();
				var polyline = event.target, path = polyline.getPath();
				polyline.setStrokeColor('#FF0000');
				map.addOverlay(marker);
				marker.setPosition(path[Math.ceil(path.length / 2)]);
				marker.openInfoWindow(infoWindow);
				$('#editSectionInfo').on('click', function() {
					operation.editSection(section.id);
				});
				$('#movePolyline').on('click', function() {
					operation.editPolyline(section.id);
				});
			});
		},
		editPolyline : function(sectionId) {
			var overlays = map.getOverlays();
			overlays.forEach(function (overlay) {
				if (overlay.cdata && overlay.cdata.id == sectionId) {
					overlay.enableEditing();
					overlay.addEventListener('rightclick', operation.confirmPolyline);
					layer.msg('拖动路段至相应位置,然后右击确定!');
				}
			});
		},
		confirmPolyline: function (event) {
			var polyline = event.target;
			polyline.removeEventListener('rightclick', operation.confirmPolyline);
			polyline.disableEditing();
			var points = polyline.getPath(), bsectionVectorWkt = new Array();
			for (var i = 0;i < points.length;i++) {
				bsectionVectorWkt.push(points[i].lng + ' ' + points[i].lat);
			}
			SectionService.sectionUpdate({id: polyline.cdata.id, bsectionVectorWkt: 'LINESTRING(' + bsectionVectorWkt.join(',') + ')'}, function(result) {
				if (result.status == 'SUCCESS') {
					layer.msg('修改成功...');
				} else {
					layer.msg('修改失败...');
				}
			});
		},
		editSection: function(sectionId) {
			layer.closeAll();
			var overlays = map.getOverlays();
			overlays.forEach(function (overlay) {
				if (overlay.cdata && overlay.cdata.id == sectionId) {
					overlay.disableEditing();
					var editPloyLineArray = overlay.getPath();
					editSection = overlay.cdata;
					$.get('editsection.html', function(m){
						$(pjaxContainer).append(m);
						$('#edit_section_modal').trigger('modal.show', [SectionOperation, SectionService, editSection]);
					});
				}
			});
		},
		clearMarkAndOverlays : function() {
			map.clearOverlays();
			map.removeOverlay();
		}
	}

	return operation;
}();