使用 d3.geo.path() 在 D3 中映射弧

Mapping an arc in D3 using d3.geo.path()

我正在尝试绘制一条连接美国地图上两点的弧线。 我用来制作美国地图的代码是

var path = d3.geo.path()
    .projection(projection);

var graticule = d3.geo.graticule()
    .extent([[-98 - 45, 38 - 45], [-98 + 45, 38 + 45]])
    .step([5, 5]);

var svg = d3.select("body").append("svg")
    .attr("width", width)
    .attr("height", height);

svg.append("path")
    .datum(graticule)
    .attr("class", "graticule")
    .attr("d", path);

queue()
.defer(d3.json,'us.json')
.await(makeMyMap);



function makeMyMap(error, us) {
  if (error) throw error;

  svg.insert("path", ".graticule")
      .datum(topojson.feature(us, us.objects.land))
      .attr("class", "land")
      .attr("d", path);

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(us, us.objects.counties, function(a, b) { return a !== b && !(a.id / 1000 ^ b.id / 1000); }))
      .attr("class", "county-boundary")
      .attr("d", path);

  svg.insert("path", ".graticule")
      .datum(topojson.mesh(us, us.objects.states, function(a, b) { return a !== b; }))
      .attr("class", "state-boundary")
      .attr("d", path);

  drawPath()

}

function drawPath() {
var route = svg.insert("path", ".graticule")
                   .datum({type: "LineString", coordinates: [[33,-118], [38.6,-78]]})
                   .attr("class", "route")
                   .attr("d", path);
}

目前正在某处制作drawPath()函数绘制的路径,但我无法在地图上查看它。如果我不在 CSS 中设置 fill: none 那么屏幕将变黑,但是将其设置为一种颜色只会使 canvas 被该颜色覆盖。

us.json文件是用来制作地图的,是一个topojson对象。

您搞砸了 LineString 的位置。根据 spec 位置指定为 [longitude,latitude]。由于纬度值不能超过 90 度,显然您需要转换坐标值的顺序:

.datum({type: "LineString", coordinates: [[-118,33], [-78,38.6]]})

感谢 Mark 的评论,他花时间并付出了努力,这也可以在他的 working demo 中找到。