﻿	//====================================多边形查询类==========================================
	/*
	 * 定义多边形查询类
	 */
	function KSearchPolygonControl()
    {
        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 KSearchPolygonControl_initialize = function(mapObject)
    {
        var controlObject = this;
        if (controlObject.Map && mapObject == controlObject.Map)
        {
            return;
        }
        controlObject.Map = mapObject;
        controlObject.loaded = true;
    }
    
    /*
	 * 开始测量
	 */
    var KSearchPolygonControl_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;
        if (controlObject.mapMouseDownListener)
        {
            KEvent.removeListener(controlObject.mapMouseDownListener);
        }
        controlObject.mapMouseDownListener = KEvent.addListener(controlObject.Map, "mousedown", controlObject, this.onMapMouseDown);        
    }
    
    /*
	 * 响应鼠标点击事件
	 */
    var KSearchPolygonControl_onMapMouseDown = function(x, eventButton)
    {
        var controlObject = this;
        var index=controlObject.index;
                
        if (!controlObject.points[index])
        {
            if (eventButton == 1)	//左键
            {
				//controlObject.Map.clearOverLays(); 
                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,KSearchPolygonControl_sendMessageEnd);				
					
					//转化客户端坐标到直角坐标 
					var position=controlObject.Map.clientToMap(message);
					KEvent.trigger(controlObject.Map, "searchpolygon", [position,1]);
        
                }
                
                
                //清除移动线
                var points = new Array(0);                
                points.push([0][0]);     
                if(controlObject.lastPolyline!=null)           
					controlObject.lastPolyline.setPoints(points);
                
                //移除提示框显示
                if(controlObject.tipText!=null)  
                controlObject.Map.removeOverLay(controlObject.tipText);
                
                //删除鼠标移动侦听
                if (controlObject.mapMouseMoveListener)
				{
					KEvent.removeListener(controlObject.mapMouseMoveListener);
				}
                
                
                controlObject.index++;
                controlObject.lastPoint = null;
            }

        }
    }
    /*
	 * 响应鼠标移动事件
	 */
    var KSearchPolygonControl_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 KSearchPolygonControl_setResponseFunction  = function(functionName)
    {
		var controlObject = this;
		if (controlObject.searchPolygonListener)	KEvent.removeListener(controlObject.searchPolygonListener);
		controlObject.searchPolygonListener=KEvent.addListener(map, "searchpolygon",functionName);
    }
	
	/*
	 * 响应服务器返回值
	 */
	var KSearchPolygonControl_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 KSearchPolygonControl_getObject = function()
    {
        var controlObject = this;
        return controlObject.div;
    }
    /**
	 * 释放
	 */	
    var KSearchPolygonControl_depose = function()
    {
        var controlObject = this;        
        if (controlObject.mapMouseDownListener)
        {
            KEvent.removeListener(controlObject.mapMouseDownListener);
        }
        if (controlObject.mapMouseMoveListener)
        {
            KEvent.removeListener(controlObject.mapMouseMoveListener);
        }
       
        //删除线显示
        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 KSearchPolygonControl_setPolygonOpacity = function(value)
    {
		this.polygonOpacity=value;
    }
    
    
    
    /**
	 * 设置面边框颜色
	 */	
    var KSearchPolygonControl_setPolygonBorderColor = function(value)
    {
		this.polygonBorderColor=value;
    }
    
    /**
	 * 设置面边框宽度
	 */	
    var KSearchPolygonControl_setPolygonBorderLine = function(value)
    {
		this.polygonBorderLine=value;
    }
    
    /**
	 * 设置面背景颜色
	 */	
    var KSearchPolygonControl_setPolygonBgColor = function(value)
    {
		this.polygonBgColor=value;
    }
    
    
    /**
	 * 设置文本背景色
	 */	
    var KSearchPolygonControl_setTextBgColor = function(value)
    {
		this.textBackgroundColor=value;
    }  
    
    
    /**
	 * 设置文本边框宽度
	 */	
    var KSearchPolygonControl_setTextBorderLine = function(value)
    {
		this.textBorderLine=value;
    }
    
    /**
	 * 设置文本边框颜色
	 */	
    var KSearchPolygonControl_setTextBorderColor = function(value)
    {
		this.textBorderColor=value;
    }
    
    /**
	 * 设置文本字体大小
	 */	
    var KSearchPolygonControl_setTextFontSize = function(value)
    {
		this.textFontSize=value;
    }
    
    /**
	 * 设置文本字体颜色
	 */	
    var KSearchPolygonControl_setTextFontColor = function(value)
    {
		this.textFontColor=value;
    }
    
    /**
	 * 设置文本透明度
	 */	
    var KSearchPolygonControl_setTextOpacity = function(value)
    {
		this.textOpacity=value;
    }   
    
    
    

    KConfig.defineClass("KSearchPolygonControl", KSearchPolygonControl);
    
    KConfig.defineFunction(KSearchPolygonControl, "startup", KSearchPolygonControl_startup);
    KConfig.defineFunction(KSearchPolygonControl, "onMapMouseDown", KSearchPolygonControl_onMapMouseDown);
    KConfig.defineFunction(KSearchPolygonControl, "onMapMouseMove", KSearchPolygonControl_onMapMouseMove);
    KConfig.defineFunction(KSearchPolygonControl, "initialize", KSearchPolygonControl_initialize);
    KConfig.defineFunction(KSearchPolygonControl, "getObject", KSearchPolygonControl_getObject);
    KConfig.defineFunction(KSearchPolygonControl, "depose", KSearchPolygonControl_depose);  
    KConfig.defineFunction(KSearchPolygonControl, "setResponseFunction", KSearchPolygonControl_setResponseFunction);  
    
    KConfig.defineFunction(KSearchPolygonControl, "setPolygonOpacity", KSearchPolygonControl_setPolygonOpacity); 
    KConfig.defineFunction(KSearchPolygonControl, "setPolygonBorderColor", KSearchPolygonControl_setPolygonBorderColor); 
    KConfig.defineFunction(KSearchPolygonControl, "setPolygonBorderLine", KSearchPolygonControl_setPolygonBorderLine);
    KConfig.defineFunction(KSearchPolygonControl, "setPolygonBgColor", KSearchPolygonControl_setPolygonBgColor); 
    
    KConfig.defineFunction(KSearchPolygonControl, "setTextBgColor", KSearchPolygonControl_setTextBgColor); 
    KConfig.defineFunction(KSearchPolygonControl, "setTextBorderLine", KSearchPolygonControl_setTextBorderLine); 
    KConfig.defineFunction(KSearchPolygonControl, "setTextBorderColor", KSearchPolygonControl_setTextBorderColor);
    KConfig.defineFunction(KSearchPolygonControl, "setTextFontSize", KSearchPolygonControl_setTextFontSize); 
    KConfig.defineFunction(KSearchPolygonControl, "setTextFontColor", KSearchPolygonControl_setTextFontColor);
    KConfig.defineFunction(KSearchPolygonControl, "setTextOpacity", KSearchPolygonControl_setTextOpacity);
    
    //=================================多边形查询类[END]==========================================