给定具有航点属性的飞机数组,其中包含由空格分开的纬度和经度字符串,这是在 D3 中在地图上绘制路线的最有效方法。是否有更类似于 D3 的方法将飞机数组作为数据参数和 d(waypoints) 传递给类似于 d3.svg.line() 生成器的 d3.geo.path() 生成器。
var width = 500,
height = 500;
var svg = d3.select("body").append("svg")
.attr("width", width)
.attr("height", height);
var projection = d3.geo.mercator()
.translate([width / 2, height / 2]);
var path = d3.geo.path()
.projection(projection);
draw();
function draw(){
for (var i = 0; i < aircraft.length; i++){
svg.append("path")
.datum(parse(aircraft[i].waypoints))
.attr("class", "route")
.attr("d", path);
}
}
function parse(waypoints){
var route;
var positions = [];
var points = waypoints.split(" ");
for (var i = 0; i < points.length - 1; i = i + 2) {
positions.push([parseFloat(points[i + 1]), parseFloat(points[i])]);
}
route = {
type: "LineString",
coordinates: positions
}
return route;
}