js map 获得某一路径(任意坐标组合数组)上,n个均匀分布的坐标点

直接上代码:

/**
 * example: 
 * var map_route = new MapRoute({
 *	    x_alias : 'lon',
 *	    y_alias : 'lat',
 *	    parsePoint : function(x, y){
 *		    return new OpenLayers.LonLat(x, y);
 *	    }
 * });
 * map_route.abtainEvenPoint([......], 3);
 * 
 * @param option
 * @returns
 */
function MapRoute(options){
	
	this.x_alias = options.x_alias;
	this.y_alias = options.y_alias;
	
	this.parsePoint = options.parsePoint || function(x, y){
		return {
			x:x,
			y:y
		};
	};
}

/**
 * 获得路径上均匀分布的点
 */
MapRoute.prototype.abtainEvenPoint = function(route_arr, count){
	var arr = [];
	if(count < 1 || route_arr.length < 2){
		return arr;
	}
	arr.push(route_arr[0]);
	if(count > 2){
		var totalLength = 0;
		for ( var int = 1; int < route_arr.length; int++) {
			totalLength += this.twoPointLength(route_arr[int-1], route_arr[int]);
		}
		var evenLength = totalLength/(count-1);
		
		var iterator = this.getIteratorPoint(route_arr);
		var current_length = 0;
		var sanjiao_length = evenLength;
		var startLonLat = null;
		for ( var int2 = 0; int2 < count-2; int2++) {
			if(current_length - sanjiao_length < 0){
				sanjiao_length -= current_length;
				if(iterator.hasNextTwoPoint()){
					current_length = iterator.nextTwoPointLength();
					if(current_length - sanjiao_length < 0){
						int2 --;
						continue;
					}
					startLonLat = iterator.getCurrentPoint();
				}else{
					break;
				}
			}else{
				startLonLat = arr[arr.length-1];
			}
			current_length -= sanjiao_length;
			arr.push(this.getPoint(startLonLat, iterator.getNextPoint(), sanjiao_length));
			sanjiao_length = evenLength;
		}
		
		arr.push(route_arr[route_arr.length-1]);
	}else if(count == 2){
		arr.push(route_arr[route_arr.length-1]);
	}
	
	return arr;
};
/**
 * 得到起始点到结束点直线上,距离起始点length远的点
 */
MapRoute.prototype.getPoint = function(start, end, length){
	var to_right = end[this.x_alias] - start[this.x_alias] > 0 ? true : false;
	var to_top = end[this.y_alias] - start[this.y_alias] > 0 ? true : false;
	
	var x,y,totalLength = 0;
	x = Math.abs(end[this.x_alias] - start[this.x_alias]);
	y = Math.abs(end[this.y_alias] - start[this.y_alias]);
	totalLength += Math.sqrt(x*x+y*y);
	
	var new_x, new_y;
	if(to_right){
		new_x = start[this.x_alias]+length*x/totalLength;
	}else{
		new_x = start[this.x_alias]-length*x/totalLength;
	}
	if(to_top){
		new_y = start[this.y_alias]+length*y/totalLength;
	}else{
		new_y = start[this.y_alias]-length*y/totalLength;
	}
	
	return this.parsePoint(new_x, new_y);
};
/**
 * 2个坐标点之间直线距离
 */
MapRoute.prototype.twoPointLength = function(start, end){
	var x,y,totalLength = 0;
	x = Math.abs(end[this.x_alias] - start[this.x_alias]);
	y = Math.abs(end[this.y_alias] - start[this.y_alias]);
	totalLength += Math.sqrt(x*x+y*y);
	return totalLength;
};
MapRoute.prototype.getIteratorPoint = function(arr){
	var the = this;
	function IteratorPoint(arr){
		this.arr = arr;
		this.current_index = -1;
	};
	IteratorPoint.prototype.hasNextTwoPoint = function(){
		if(this.current_index+2 > this.arr.length-1){
			return false;
		}
		return true;
	};
	IteratorPoint.prototype.nextTwoPointLength = function(){
		if(!this.hasNextTwoPoint()){
			return -1;
		}
		this.current_index ++;
		var end = this.arr[this.current_index+1];
		var start = this.arr[this.current_index];
		
		return the.twoPointLength(start, end);
	};
	IteratorPoint.prototype.getCurrentPoint = function(){
		return this.arr[this.current_index];
	};
	IteratorPoint.prototype.getNextPoint = function(){
		return this.arr[this.current_index+1];
	};
	
	return new IteratorPoint(arr);
};

如有出入,欢迎指正……

转载于:https://my.oschina.net/u/576223/blog/273665

评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值