﻿	
	//====================================测量线距离类==========================================
	
	/*
	 * 定义测量线距离类
	 */
	function KPolygonControl()
    {
        this.loaded = false;
        this.polygonOpacity=0.5;				//面透明度
        this.polygonBorderColor="blue";			//面边框颜色
        this.polygonBorderLine=1;				//面边框宽度
        this.polygonBgColor="#99FFCC";			//面背景颜色
        
        this.textBackgroundColor="#FFFFD7";		//文本背景色
        this.textBorderLine=1;					//文本边框宽度
        this.textBorderColor="#ADAEAC";			//文本边框颜色
        this.textFontSize=12;					//文本字体大小
        this.textFontColor="#993300";			//文本字体颜色
        this.textOpacity=1;						//文本透明度
    }
    
    /*
	 * 初始化
	 */
    var KPolygonControl_initialize = function(mapObject)
    {
        var controlObject = this;
        if (controlObject.Map && mapObject == controlObject.Map)
        {
            return;
        }
        controlObject.Map = mapObject;
        controlObject.loaded = true;
    }
    
    /*
	 * 开始测量
	 */
    var KPolygonControl_startup = function()
    {
        var controlObject = this;        
        if (!controlObject.loaded) return;
        
		if (controlObject.Map.currTool!=null)		
        {
			controlObject.Map.currTool.depose();				 
		}
				
		controlObject.Map.currTool=this;	
		controlObject.Map.clearMouseScroll();	//清除鼠标滚动
				
		KBase.setCursorStyle(controlObject.Map.mapsDiv, "pen.cur");		
		//初始化属性
		controlObject.points = new Array(0);
		controlObject.mapTexts = new Array(0);   
        controlObject.polylines = new Array(0);
        controlObject.bounds = new Array(0);        
        controlObject.index = 0;
        
        
        controlObject.mapMouseDownListener = KEvent.addListener(controlObject.Map, "mousedown", controlObject, this.onMapMouseDown);        
        
         //地图变化事件--重新开始
        controlObject.mapChangeListener = KEvent.addListener(controlObject.Map, "mapload", controlObject, this.startup);        
        
    }
    
    /*
	 * 响应鼠标点击事件
	 */
    var KPolygonControl_onMapMouseDown = function(x, eventButton)
    {
        var controlObject = this;
        var index=controlObject.index;
                
        if (!controlObject.points[index])
        {
            if (eventButton == 1)	//左键
            {
                controlObject.points.push(new Array(0));		//新增一个组点               
                controlObject.points[index].push(x); 
                
                //创建面积提示文本框
                var mapText = new KMapText(x);
                mapText.setNoWrap(true);
                mapText.setVisible(false);
                mapText.setBgColor(controlObject.textBackgroundColor);
                mapText.setBorderLine(controlObject.textBorderLine);
                mapText.setBorderColor(controlObject.textBorderColor);
                mapText.setFontSize(controlObject.textFontSize);
                mapText.setFontColor(controlObject.textFontColor);
                mapText.setOpacity(controlObject.textOpacity);
                controlObject.Map.addOverLay(mapText);
                controlObject.mapTexts.push(mapText);
                               
                //创建线对象
                var polyLine=new KPolygon(controlObject.points[index],controlObject.polygonBorderColor,controlObject.polygonBgColor,controlObject.polygonBorderLine,controlObject.polygonOpacity);                             
                controlObject.Map.addOverLay(polyLine);
                
                controlObject.polylines.push(polyLine);               
                
                controlObject.lastPoint = x;//设置最后一个点             
                
                if (!controlObject.tipText)
                {
                    controlObject.tipText = new KMapText(controlObject.points[controlObject.index][0]);
                    controlObject.tipText.setBgColor("#D2F0FF");
                    controlObject.tipText.setFontSize(12);                    
                    controlObject.tipText.setNoWrap(true);                    
					controlObject.tipText.setText("点击右键结束");
                }
                controlObject.Map.addOverLay(controlObject.tipText);
                
                
                //新增点边界组并设置X、Y最大最小值
                controlObject.bounds.push(new Array(0));
                controlObject.bounds[index][0]=x[0];
                controlObject.bounds[index][1]=x[1];
                controlObject.bounds[index][2]=x[0];
                controlObject.bounds[index][3]=x[1];
                
                
                
                //增加地图容器鼠标移动侦听            
                controlObject.mapMouseMoveListener = KEvent.addListener(controlObject.Map.container, "mousemove", controlObject, this.onMapMouseMove);        
            }

        }
        else
        {
            // 经过点
            if (eventButton == 1)
            {
           
                controlObject.points[index].push(x);          
                controlObject.polylines[index].setPoints(controlObject.points[index]);            
                controlObject.lastPoint = x;
                //设置X、Y最大最小值 
                controlObject.bounds[index][0]=Math.min(controlObject.bounds[index][0],x[0]);
                controlObject.bounds[index][1]=Math.min(controlObject.bounds[index][1],x[1]);
                controlObject.bounds[index][2]=Math.max(controlObject.bounds[index][2],x[0]);
                controlObject.bounds[index][3]=Math.max(controlObject.bounds[index][3],x[1]);          
                
            }
            else
            {
                controlObject.lastIndex=controlObject.index;//定义上一个次数
                
                var message="";
                if (controlObject.points[index].length>2)
                {  
					for (i=0;i<controlObject.points[index].length;i++)
					{
						if (i==0)
							message += controlObject.points[index][i][0] + ";" + controlObject.points[index][i][1];
						else
							message += ";" + controlObject.points[index][i][0] + ";" + controlObject.points[index][i][1];
					}
					
					var sendMessage=KBase.createSendXml(this.Map.userId,"Polygon",message);			
					KRequest.send(this,this.Map.mapCmd,sendMessage,KPolygonControl_sendMessageEnd);				
                }
                
                
                //清除移动线
                var points = new Array(0);                
                points.push([0][0]);                
                controlObject.lastPolyline.setPoints(points);
                
                //移除提示框显示
                controlObject.Map.removeOverLay(controlObject.tipText);
                
                //删除鼠标移动侦听
                KEvent.removeListener(controlObject.mapMouseMoveListener);
                
                controlObject.index++;
                controlObject.lastPoint = null;
            }

        }
    }
    /*
	 * 响应鼠标移动事件
	 */
    var KPolygonControl_onMapMouseMove = function(x)
    {        
        var controlObject = this;        
        if (!controlObject.lastPoint)	return;
        
        //创建移动线
        if (!controlObject.lastPolyline)
        {
            controlObject.lastPolyline = new KPolyLine(controlObject.lastPoint, null, 2);
            controlObject.lastPolyline.setLineColor("gray");
            controlObject.Map.addOverLay(controlObject.lastPolyline);             
        }        
        
        var surrPoint=KBase.getEventPosition(x, controlObject.Map.container);
        //设置移动线位置 
		var points = new Array(0); 
        points.push(controlObject.lastPoint);        
        points.push(surrPoint); 
        controlObject.lastPolyline.setPoints(points);
        //移动提示框位置
        surrPoint[0]=surrPoint[0]+12;
        surrPoint[1]=surrPoint[1]-12;
        controlObject.tipText.setPoint(surrPoint);
    }   
	
	/*
	 * 响应服务器返回值
	 */
	var KPolygonControl_sendMessageEnd=function(parameter)
	{		
		var controlObject=this.sendObject;
		var index=controlObject.lastIndex;
		var message=KBase.getMessage(this.request.responseText);	
			
		if (!message.isError)
		{
			//计算中心点
			var centerX=(controlObject.bounds[index][0]+controlObject.bounds[index][2])/2;
			var centerY=(controlObject.bounds[index][1]+controlObject.bounds[index][3])/2;	
			
			var points=new Array(0);			
			points.push(centerX);
			points.push(centerY);				
			
			controlObject.mapTexts[index].setText(message.text);
			controlObject.mapTexts[index].setPoint(points);
			controlObject.mapTexts[index].setVisible(true);
		}		
	}
	/**
	 * 获取HTML控件
	 */	
	var KPolygonControl_getObject = function()
    {
        var controlObject = this;
        return controlObject.div;
    }
    /**
	 * 释放
	 */	
    var KPolygonControl_depose = function()
    {
        var controlObject = this;        
        if (controlObject.mapMouseDownListener)
        {
            KEvent.removeListener(controlObject.mapMouseDownListener);
        }
        if (controlObject.mapMouseMoveListener)
        {
            KEvent.removeListener(controlObject.mapMouseMoveListener);
        }
        if (controlObject.mapChangeListener)
        {
			KEvent.removeListener(controlObject.mapChangeListener);
        }
        //删除线显示
        for (var i = 0; i < controlObject.polylines.length;i++)
        {
			controlObject.Map.removeOverLay(controlObject.polylines[i]);   
			controlObject.polylines[i]=null;
        }
        //删除提示框显示
        for (var i = 0; i < controlObject.mapTexts.length;i++)
        {			
			controlObject.Map.removeOverLay(controlObject.mapTexts[i]);   
			controlObject.mapTexts[i]=null;
			
        }     
        //删除移动线
        if (controlObject.lastPolyline)
        {
            controlObject.Map.removeOverLay(controlObject.lastPolyline);
            controlObject.lastPolyline = null;
        }
        //删除
        if (controlObject.tipText)
        {
            controlObject.Map.removeOverLay(controlObject.tipText);
            controlObject.tipText = null;
        }
    }   
       
    
    /**
	 * 设置面透明度
	 */	
    var KPolygonControl_setPolygonOpacity = function(value)
    {
		this.polygonOpacity=value;
    }
    
    
    
    /**
	 * 设置面边框颜色
	 */	
    var KPolygonControl_setPolygonBorderColor = function(value)
    {
		this.polygonBorderColor=value;
    }
    
    /**
	 * 设置面边框宽度
	 */	
    var KPolygonControl_setPolygonBorderLine = function(value)
    {
		this.polygonBorderLine=value;
    }
    
    /**
	 * 设置面背景颜色
	 */	
    var KPolygonControl_setPolygonBgColor = function(value)
    {
		this.polygonBgColor=value;
    }
    
    
    /**
	 * 设置文本背景色
	 */	
    var KPolygonControl_setTextBgColor = function(value)
    {
		this.textBackgroundColor=value;
    }  
    
    
    /**
	 * 设置文本边框宽度
	 */	
    var KPolygonControl_setTextBorderLine = function(value)
    {
		this.textBorderLine=value;
    }
    
    /**
	 * 设置文本边框颜色
	 */	
    var KPolygonControl_setTextBorderColor = function(value)
    {
		this.textBorderColor=value;
    }
    
    /**
	 * 设置文本字体大小
	 */	
    var KPolygonControl_setTextFontSize = function(value)
    {
		this.textFontSize=value;
    }
    
    /**
	 * 设置文本字体颜色
	 */	
    var KPolygonControl_setTextFontColor = function(value)
    {
		this.textFontColor=value;
    }
    
    /**
	 * 设置文本透明度
	 */	
    var KPolygonControl_setTextOpacity = function(value)
    {
		this.textOpacity=value;
    }   
    
    
    

    KConfig.defineClass("KPolygonControl", KPolygonControl);
    
    KConfig.defineFunction(KPolygonControl, "startup", KPolygonControl_startup);
    KConfig.defineFunction(KPolygonControl, "onMapMouseDown", KPolygonControl_onMapMouseDown);
    KConfig.defineFunction(KPolygonControl, "onMapMouseMove", KPolygonControl_onMapMouseMove);
    KConfig.defineFunction(KPolygonControl, "initialize", KPolygonControl_initialize);
    KConfig.defineFunction(KPolygonControl, "getObject", KPolygonControl_getObject);
    KConfig.defineFunction(KPolygonControl, "depose", KPolygonControl_depose);  
    
    KConfig.defineFunction(KPolygonControl, "setPolygonOpacity", KPolygonControl_setPolygonOpacity); 
    KConfig.defineFunction(KPolygonControl, "setPolygonBorderColor", KPolygonControl_setPolygonBorderColor); 
    KConfig.defineFunction(KPolygonControl, "setPolygonBorderLine", KPolygonControl_setPolygonBorderLine);
    KConfig.defineFunction(KPolygonControl, "setPolygonBgColor", KPolygonControl_setPolygonBgColor); 
    
    KConfig.defineFunction(KPolygonControl, "setTextBgColor", KPolygonControl_setTextBgColor); 
    KConfig.defineFunction(KPolygonControl, "setTextBorderLine", KPolygonControl_setTextBorderLine); 
    KConfig.defineFunction(KPolygonControl, "setTextBorderColor", KPolygonControl_setTextBorderColor);
    KConfig.defineFunction(KPolygonControl, "setTextFontSize", KPolygonControl_setTextFontSize); 
    KConfig.defineFunction(KPolygonControl, "setTextFontColor", KPolygonControl_setTextFontColor);
    KConfig.defineFunction(KPolygonControl, "setTextOpacity", KPolygonControl_setTextOpacity);
    
    //=================================测量线距离类[END]==========================================