How to move points in an orthogonal map?

前端 未结 1 520
感动是毒
感动是毒 2021-01-24 18:20

I am trying to add red points at certain geolocations on the following map created by Mike Bostock. https://bl.ocks.org/mbostock/3795040. My points show up but don\'t move with

相关标签:
1条回答
  • 2021-01-24 19:25

    You have to include the circles in the mousemove function:

    svg.on("mousemove", function() {
        var p = d3.mouse(this);
        projection.rotate([λ(p[0]), φ(p[1])]);
        svg.selectAll("path").attr("d", path);
    
        //change the circles' positions here:
        svg.selectAll("circle").attr("cx", function(d) {
                console.log(projection(d));
                return projection(d)[0];
            })
            .attr("cy", function(d) {
                return projection(d)[1];
            })
    });
    

    Here is the updated bl.ocks: https://bl.ocks.org/anonymous/2a6f07cdc12838b296674470ad715bbe/54d6de8d73347081f900c88a203019df74f23ade

    PS: Some circles appear to move wrongly: they are correct, though. The thing is that they are behind the globe. To hide those circles you'll have to write another function (which is outside the scope of this answer).

    An alternative to hide the circles behind the globe is using a path instead a circle. That way, the projection will automatically clip those paths. Have a look: https://bl.ocks.org/anonymous/9e640195e2c021cd79b5ca9b2238a44c/1c43719a7d6a85d0226cf3c468ac23e570add22d

    0 讨论(0)
提交回复
热议问题