直接上代码:
/**
* 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);
};
如有出入,欢迎指正……