⭐ 欢迎来到虫虫下载站! | 📦 资源下载 📁 资源专辑 ℹ️ 关于我们
⭐ 虫虫下载站

📄 toolnetscape.js

📁 c#中用MapXtreme开发的地理信息系统
💻 JS
📖 第 1 页 / 共 2 页
字号:

//////////////////////////////////////////////////////////////////////////////////////////
// Javascript client side drawing code for Netscape and Mozilla browsers 

function MapInfoWebLog(msg)
{
	var t = document.getElementById("t1");
	if (t != null) {
		t.value = msg;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// Globals
var color='black';

//////////////////////////////////////////////////////////////////////////////////////////
// Returns map object given name
function MapInfoWebGetMap(mapid)
{
	var _map =document.getElementById(mapid +"_Image");
	return _map;
}

// API to set current tool on the maptools. This method should be used by users to set current tool
// on the client side
function MapInfoWebSetCurrentTool(mapid, toolName)
{
	var _map =MapInfoWebGetMap(mapid);
	if (_map.MapTools.CurrentTool != null) {
		_map.MapTools.CurrentTool.Stop(_map);
	}
	for (i = 0; i < _map.MapTools.Tools.length; i++) {
		var tool = _map.MapTools.Tools[i];
		if (toolName == tool.Name) {
			_map.MapTools.CurrentTool = tool;
			tool.Start(_map);
		}
	}
}

// Return current tool object
function MapInfoWebGetCurrentTool(mapid)
{
	var _map =MapInfoWebGetMap(mapid);
	if (_map != null && _map.MapTools != null) {
		return _map.MapTools.CurrentTool;
	}
}

/////////////////////////////////////////////////////////////////////////////////////////
// DHTML related methods:

function MapInfoWebAddElement(element, id)
{
	var obj = document.createElement(element);
	obj.id = id;
	return(obj);
}

function MapInfoWebGetAbsolutePosition(elem)
{
	var left = elem.offsetLeft;
	var top = elem.offsetTop;
	if(elem.offsetParent)
	{
		pt = MapInfoWebGetAbsolutePosition(elem.offsetParent);
		left += pt.x;
		top += pt.y;
	}
	return new MapInfoWebPoint(left, top);
}

////////////////////////
// DHTML related methods
function MapInfoWebmkDiv(object, x, y, w, h)
{
	x = x - object.left;
	y = y - object.top;
	var divObj = MapInfoWebAddElement("DIV", "");
	divObj.style.position = 'absolute';
	divObj.style.left = x + 'px'; 
	divObj.style.top = y + 'px'; 
	divObj.style.width = w + 'px'; 
	divObj.style.height = h + 'px';
	divObj.style.clip = 'rect(0,'+w+'px,'+h+'px,0)';
	divObj.style.overflow = 'hidden';
	divObj.style.backgroundColor = color;
	object.appendChild(divObj);
}

function MapInfoWebmkOvQds(object,cx, cy, xl, xr, yt, yb, w, h)
{
	this.MapInfoWebmkDiv(object,xr+cx, yt+cy, w, h);
	this.MapInfoWebmkDiv(object,xr+cx, yb+cy, w, h);
	this.MapInfoWebmkDiv(object,xl+cx, yb+cy, w, h);
	this.MapInfoWebmkDiv(object,xl+cx, yt+cy, w, h);
}

function MapInfoWebmkLin(object, x1, y1, x2, y2)
{
	if (x1 > x2)
	{
		var _x2 = x2;
		var _y2 = y2;
		x2 = x1;
		y2 = y1;
		x1 = _x2;
		y1 = _y2;
	}
	var dx = x2-x1, dy = Math.abs(y2-y1),
	x = x1, y = y1,
	yIncr = (y1 > y2)? -1 : 1;

	if (dx >= dy)
	{
		var pr = dy<<1,
		pru = pr - (dx<<1),
		p = pr-dx,
		ox = x;
		while ((dx--) > 0)
		{
			++x;
			if (p > 0)
			{
				MapInfoWebmkDiv(object, ox, y, x-ox, 1);
				y += yIncr;
				p += pru;
				ox = x;
			}
			else p += pr;
		}
		MapInfoWebmkDiv(object, ox, y, x2-ox+1, 1);
	}

	else
	{
		var pr = dx<<1,
		pru = pr - (dy<<1),
		p = pr-dy,
		oy = y;
		if (y2 <= y1)
		{
			while ((dy--) > 0)
			{
				if (p > 0)
				{
					MapInfoWebmkDiv(object, x++, y, 1, oy-y);
					y += yIncr;
					p += pru;
					oy = y;
				}
				else
				{
					y += yIncr;
					p += pr;
				}
			}
			MapInfoWebmkDiv(object, x2, y2, 1, oy-y2);
		}
		else
		{
			while ((dy--) > 0)
			{
				y += yIncr;
				if (p > 0)
				{
					MapInfoWebmkDiv(object, x++, oy, 1, y-oy);
					p += pru;
					oy = y;
				}
				else p += pr;
			}
			MapInfoWebmkDiv(object, x2, oy, 1, y2-oy);
		}
	}
}

function MapInfoWebmkRect(object, x, y, w, h)
{
	var stroke = 1;
	MapInfoWebmkDiv(object, x, y, w, stroke);
	MapInfoWebmkDiv(object, x+w, y, stroke, h);
	MapInfoWebmkDiv(object, x, y+h, w+stroke, stroke);
	MapInfoWebmkDiv(object, x, y+stroke, stroke, h-stroke);
}

function MapInfoWebmkEllipse(object, left, top, width, height)
{
	var a = width>>1, b = height>>1,
	wod = width&1, hod = (height&1)+1,
	cx = left+a, cy = top+b,
	x = 0, y = b,
	ox = 0, oy = b,
	aa = (a*a)<<1, bb = (b*b)<<1,
	st = (aa>>1)*(1-(b<<1)) + bb,
	tt = (bb>>1) - aa*((b<<1)-1),
	w, h;
	while (y > 0)
	{
		if (st < 0)
		{
			st += bb*((x<<1)+0x3);
			tt += (bb<<1)*(++x);
		}
		else if (tt < 0)
		{
			st += bb*((x<<1)+0x3) - (aa<<1)*(y-1);
			tt += (bb<<1)*(++x) - aa*(((y--)<<1)-0x3);
			w = x-ox;
			h = oy-y;
			if (w&0x2 && h&0x2)
			{
				MapInfoWebmkOvQds(object,cx, cy, -x+0x2, ox+wod, -oy, oy-1+hod, 1, 1);
				MapInfoWebmkOvQds(object,cx, cy, -x+1, x-1+wod, -y-1, y+hod, 1, 1);
			}
			else MapInfoWebmkOvQds(object,cx, cy, -x+1, ox+wod, -oy, oy-h+hod, w, h);
			ox = x;
			oy = y;
		}
		else
		{
			tt -= aa*((y<<1)-0x3);
			st -= (aa<<1)*(--y);
		}
	}
	MapInfoWebmkDiv(object,cx-a, cy-oy, a-ox+1, (oy<<1)+hod);
	MapInfoWebmkDiv(object,cx+ox+wod, cy-oy, a-ox+1, (oy<<1)+hod);
}

function MapInfoWebmkPolyline(object, x, y)
{
	var i = x.length-1; 
	while (i >= 0) {
		MapInfoWebmkLin(object, x[i], y[i], x[--i], y[i]);
	}
}

function MapInfoWebUpdateLine(map, currentPoint)
{
	// Clip the current point based on the size of the image:
	currentPoint.x = Math.max(map.origin.x, Math.min(currentPoint.x, map.origin.x + map.offsetWidth+2));
	currentPoint.y = Math.max(map.origin.y, Math.min(currentPoint.y, map.origin.y + map.offsetHeight+2));
	
	// Set the starting and ending points
	line = document.GetElementById("MapInfoWebEntity");
	line.innerHTML = '';
	var startPoint = map.GetPoint(0);
	MapInfoWebmkLin(line, startPoint.x, startPoint.y, currentPoint.x, currentPoint.y);
}

function MapInfoWebUpdateRectangle(map, startPoint, currentPoint)
{
	var rect = document.getElementById("MapInfoWebEntity");
	if(!rect)
	{	
		// TODO Should we expose line styles at the tool level???
		var rect = MapInfoWebAddElement("DIV", "RubberRect");
		rect.style.position = 'absolute';
		rect.style.visibility = 'visible';
		rect.id = "MapInfoWebEntity";
		
		
		// Set the rect style to the map parent's z-index:
		rect.style.zIndex = map.parentNode.style.zIndex + 200;
		
		// Add the rect to the document body:
		document.body.appendChild(rect);
		rect.style.left = rect.left = startPoint.x;
		rect.style.top = rect.top = startPoint.y;
		
		// Add event handlers for the element
		rect.onmousedown = MapInfoWebRectangleOnMouseDown;
		rect.onmousemove = MapInfoWebRectangleOnMouseMove;
		rect.onmouseup = MapInfoWebRectangleOnMouseUp;

		rect.map = map;
	}
	
	// Convert the start and current points (which are offset by the map) to absolute
	// positions:
	rect.innerHTML = '';
	//startPoint.x += map.origin.x;
	//startPoint.y += map.origin.y;
	//currentPoint.x += map.origin.x;
	//currentPoint.y += map.origin.y;
	

	// Clip the current point based on the size of the image:
	currentPoint.x = Math.max(map.origin.x, Math.min(currentPoint.x, map.origin.x + map.offsetWidth+2));
	currentPoint.y = Math.max(map.origin.y, Math.min(currentPoint.y, map.origin.y + map.offsetHeight+2));
	
	// Set the rect position based on the adjusted start and current points:
	MapInfoWebmkRect(rect, Math.min(startPoint.x, currentPoint.x), Math.min(startPoint.y, currentPoint.y),
					 Math.abs(currentPoint.x - startPoint.x), Math.abs(currentPoint.y - startPoint.y));
}

function MapInfoWebRemoveRectangle(map)
{
	var rect = document.getElementById("MapInfoWebEntity");
	if(rect)
	{
		document.body.removeChild(rect);
	}
}

function MapInfoWebUpdateCircle(map, startPoint, currentPoint)
{
	// TODO Try/catch

	// If a circle VML hasn't been created:
	var circle = document.getElementById("MapInfoWebEntity");
	if(!circle)
	{	
		// TODO Should we expose line styles at the tool level???
		// Create a vml circle with absolute positioning:
		circle = MapInfoWebAddElement("DIV", "RubberCir");
		circle.style.position = "absolute";
		circle.style.visibility = 'visible';
		circle.id = "MapInfoWebEntity";
		
		// Set the rect style to the map parent's z-index:
		circle.style.zIndex = map.parentNode.style.zIndex + 200;
		
		// Add the rect to the document body:
		document.body.appendChild(circle);
		
		// Add event handlers for the element
		circle.onmousedown = MapInfoWebToolOnMouseDown;
		circle.onmousemove = MapInfoWebCircleOnMouseMove;
		circle.onmouseup = MapInfoWebCircleOnMouseUp;
		document.onmousemove = MapInfoWebCircleOnMouseMove;
		document.onmouseup = MapInfoWebCircleOnMouseUp;
		
		circle.map = map;
		document.map = map;
	}

	circle.innerHTML = '';

	// Clip the current point based on the size of the image:
	//currentPoint.x = Math.max(map.origin.x, Math.min(currentPoint.x, map.origin.x + map.offsetWidth+2));
	//currentPoint.y = Math.max(map.origin.y, Math.min(currentPoint.y, map.origin.y + map.offsetHeight+2));

	// Radius is the distance between the center and the current point
	var radius = Math.sqrt(Math.pow((currentPoint.x - startPoint.x), 2) + Math.pow((currentPoint.y - startPoint.y), 2))

	// The left and top are the center minus the radius
	circle.style.left = circle.left = startPoint.x - radius;
	circle.style.top = circle.top = startPoint.y - radius;
	
	// The width and height are the same as the diameter
	circle.style.width = 2*radius;
	circle.style.height =  circle.style.width;
	
	// Check to see if circle goes out of bounds, and if so, set the clipping 
	// parameters
	
	var clipTop, clipRight, clipBottom, clipLeft;
	currentLeft = parseInt(circle.style.left);
	currentTop = parseInt(circle.style.top);
	currentRight = currentLeft + parseInt(circle.style.width);
	currentBottom = currentTop + parseInt(circle.style.height);
		
	if ( currentTop > map.origin.y )
		clipTop = 'auto';
	else
		clipTop = map.origin.y - currentTop;
			
	if (currentRight > (map.origin.x + map.width)) 
		clipRight = (map.origin.x + map.width) - currentLeft;
	else 
		clipRight =  'auto';
		
	if (currentBottom > (map.origin.y + map.height)) 
		clipBottom = (map.origin.y + map.height) - currentTop;
	else 
		clipBottom =  'auto';
			
	if (currentLeft > map.origin.x) 
		clipLeft = 'auto';
	else 
		clipLeft = map.origin.x - currentLeft;
	
	circle.style.clip = 'rect(' + clipTop + ' ' +  clipRight + ' ' + clipBottom + ' ' + clipLeft +')';
	MapInfoWebmkEllipse(circle, startPoint.x - radius, startPoint.y - radius, radius*2, radius*2);
}

function MapInfoWebUpdatePolygon(map, currentPoint, doPolygon)
{
	// Clip the current point based on the size of the image:
	currentPoint.x = Math.max(map.origin.x, Math.min(currentPoint.x, map.origin.x + map.offsetWidth+2));
	currentPoint.y = Math.max(map.origin.y, Math.min(currentPoint.y, map.origin.y + map.offsetHeight+2));

	// Now set the polylines points collection so that it draws the segments
	// it contains all the previously clicked points + current point (which is moving)
	// + the first point to complete the polygon
	var line = document.getElementById("MapInfoWebEntity");

	var tempX = new Array();
	var tempY = new Array();
	for (i=0; i<map.PointsData.Points.length; i++) {
		var pt = map.PointsData.GetPoint(i);
		tempX[i] = pt.x;
		tempY[i] = pt.y;
	}
	
	tempX[tempX.length]  = currentPoint.x;
	tempY[tempY.length]  = currentPoint.y;
	if (doPolygon) {
		var firstPoint = map.PointsData.GetPoint(0);
		tempX[tempX.length]  = firstPoint.x;
		tempY[tempY.length]  = firstPoint.y;
	}	
	
	line.innerHTML = '';
	MapInfoWebmkPolyline(line, tempX, tempY);
}

// Method to pan the image and clip if goes out of bounds
function MapInfoWebPanImage (map, startPoint, currentPoint)
{
	var clipTop, clipRight, clipBottom, clipLeft;
	var currentLeft, currentTop, currentRight, currentBottom;
	
	// Calculate absolute current coordinates of the image
	currentLeft = map.origin.x + (currentPoint.x - startPoint.x);
	currentTop = map.origin.y + (currentPoint.y - startPoint.y);
	currentRight = currentLeft + map.width;
	currentBottom = currentTop + map.height;
		
	// Check to see if image goes out of bounds, and if so, set the clipping 
	// parameters
	if ( currentTop > map.origin.y )
		clipTop = 0;
	else
		clipTop = map.origin.y - currentTop;
			
	if (currentRight > (map.origin.x + map.width)) 
		clipRight = (map.origin.x + map.width) - currentLeft - map.BorderWidth;
	else 
		clipRight = currentRight - currentLeft - map.BorderWidth;
		
	if (currentBottom > (map.origin.y + map.height)) 
		clipBottom = (map.origin.y + map.height) - currentTop - map.BorderWidth;
	else 
		clipBottom = currentBottom - currentTop - map.BorderWidth;
			
	if (currentLeft > map.origin.x) 
		clipLeft = 0;
	else 
		clipLeft = map.origin.x - currentLeft;

	// Set the map image's style parameters to actually move the image	
	map.style.position = "absolute";
	map.style.left = currentLeft - map.origin.x; 
	map.style.top = currentTop - map.origin.y;	
	map.style.clip = 'rect(' + clipTop + ' ' +  clipRight + ' ' + clipBottom + ' ' + clipLeft +')';
}

//////////////////////////////////////////////////////////////////////////////////////////
// Common methods:

function MapInfoWebGetMapInternal(object)
{
	var map = null;
	if (object.map) {
		map = object.map;
	} else {
		map = object;
	}
	
	return map;
}

//////////////////////////////////////////////////////////////////////////////////////////
// Point Data object methods

// Append the point to points collection
function MapInfoWebAddPoint(x, y)
{
	this.Points[this.Points.length] = new MapInfoWebPoint(x,y);
}

// Get point from points collection
function MapInfoWebGetPoint(indx)
{
	if (this.Points[indx] != null) {
		return new MapInfoWebPoint(this.Points[indx].x, this.Points[indx].y);
	} else {
		return null;
	}
}

// Return the string representation of point collection, The format is "numpts,x y,x y,x y...,x y"
function MapInfoWebGetPointsString(offset)
{
	var pointString = "" + this.Points.length + ",";
	for (i = 0; i < this.Points.length; i++) {
		if (i>0)
		{
			pointString += ","
		}
		if (this.Points[i].y != -99999) {
			pointString += (this.Points[i].x - offset.x) + " " + (this.Points[i].y - offset.y);	
		} else {
			pointString += (this.Points[i].x) + " " + (this.Points[i].y);	
		}
	}
	return pointString;
}

// Return the string representation of point collection, The format is "x,y x,y x,y ... x,y"
function MapInfoWebGetPointsValue(offset)
{
	var pointString = "";
	for (i = 0; i < this.Points.length; i++) {
		if (i>0)

⌨️ 快捷键说明

复制代码 Ctrl + C
搜索代码 Ctrl + F
全屏模式 F11
切换主题 Ctrl + Shift + D
显示快捷键 ?
增大字号 Ctrl + =
减小字号 Ctrl + -