D3-Tile并非所有瓷砖加载

时间:2018-03-07 21:01:04

标签: javascript d3.js tile

我正在尝试使用D3 v4和d3-tile v0.0.4重现this example(如this question中所述,有一些小的调整要做)。为简单起见,我删除了栅格矢量并将窗口大小设置为960x500。

在缩放和平移的多个位置,左上方的图块将不会加载。任何想法为什么?我需要选择不同的窗口尺寸吗?我建议全屏展开代码段以查看问题。

当我运行基本相同的xample但使用d3-tile v0.0时,问题就会消失,因此更新中的某些内容必须更改。

<!DOCTYPE html>
<meta charset="utf-8">
<style>
    body {
        margin: 0;
    }

    path {
        fill: none;
        stroke: red;
        stroke-linejoin: round;
        stroke-width: 1.5px;
    }
</style>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-tile@0.0.4/build/d3-tile.js"></script>
<script>
    var pi = Math.PI,
        tau = 2 * pi;

    var width = 960;
        height = 500;

    // Initialize the projection to fit the world in a 1×1 square centered at the origin.
    var projection = d3.geoMercator()
        .scale(1 / tau)
        .translate([0, 0]);

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

    var tile = d3.tile()
        .size([width, height]);

    var zoom = d3.zoom()
        .scaleExtent([1 << 11, 1 << 14])
        .on("zoom", zoomed);

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

    var raster = svg.append("g");

    // Compute the projected initial center.
    var center = projection([-98.5, 39.5]);

    // Apply a zoom transform equivalent to projection.{scale,translate,center}.
    svg
        .call(zoom)
        .call(zoom.transform, d3.zoomIdentity
            .translate(width / 2, height / 2)
            .scale(1 << 12)
            .translate(-center[0], -center[1]));

    function zoomed() {
        var transform = d3.event.transform;

        var tiles = tile
            .scale(transform.k)
            .translate([transform.x, transform.y])
            ();

        projection
            .scale(transform.k / tau)
            .translate([transform.x, transform.y]);

        var image = raster
            .attr("transform", stringify(tiles.scale, tiles.translate))
            .selectAll("image")
            .data(tiles, function(d) {
                return d;
            });

        image.exit().remove();

        image.enter().append("image")
            .attr('xlink:href', function(d) {
                return 'http://' + 'abc' [d.y % 3] + '.tile.openstreetmap.org/' +
                    d.z + '/' + d.x + '/' + d.y + '.png';
            })
            .attr('x', function(d) {
                return d.x * 256;
            })
            .attr('y', function(d) {
                return d.y * 256;
            })
            .attr("width", 256)
            .attr("height", 256);
    }

    function stringify(scale, translate) {
        var k = scale / 256,
            r = scale % 1 ? Number : Math.round;
        return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
    }
</script>

1 个答案:

答案 0 :(得分:2)

所有瓷砖均来自我所看到的。如果你看一下DOM,就会有适当数量的瓷砖,只是第一个瓷砖看起来好像它永远不会正常退出(应该如此);尽管如此,它被分配了新的数据。但是,由于您不使用更新选择,因此它永远不会更新首次绘制的位置。如果向西滚动,则会导致图像重叠;如果向东滚动,则会导致屏幕外的隐藏图像。

一个解决方案,就是使用完整的退出/进入/合并/更新周期,但这会产生令人不满意的结果,即不知道为什么会出现意外行为:

<!DOCTYPE html>
<meta charset="utf-8">
<style>
    body {
        margin: 0;
    }

    path {
        fill: none;
        stroke: red;
        stroke-linejoin: round;
        stroke-width: 1.5px;
    }
</style>
<svg></svg>
<script src="https://d3js.org/d3.v4.min.js"></script>
<script src="https://unpkg.com/d3-tile@0.0.4/build/d3-tile.js"></script>
<script>
    var pi = Math.PI,
        tau = 2 * pi;

    var width = 960;
        height = 500;

    // Initialize the projection to fit the world in a 1×1 square centered at the origin.
    var projection = d3.geoMercator()
        .scale(1 / tau)
        .translate([0, 0]);

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

    var tile = d3.tile()
        .size([width, height]);

    var zoom = d3.zoom()
        .scaleExtent([1 << 11, 1 << 14])
        .on("zoom", zoomed);

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

    var raster = svg.append("g");

    // Compute the projected initial center.
    var center = projection([-98.5, 39.5]);

    // Apply a zoom transform equivalent to projection.{scale,translate,center}.
    svg
        .call(zoom)
        .call(zoom.transform, d3.zoomIdentity
            .translate(width / 2, height / 2)
            .scale(1 << 12)
            .translate(-center[0], -center[1]));

    function zoomed() {
        var transform = d3.event.transform;

        var tiles = tile
            .scale(transform.k)
            .translate([transform.x, transform.y])
            ();

        projection
            .scale(transform.k / tau)
            .translate([transform.x, transform.y]);

        var image = raster
            .attr("transform", stringify(tiles.scale, tiles.translate))
            .selectAll("image")
            .data(tiles, function(d) {
                return d;
            });

        image.exit().remove();
        // enter:
        var entered = image.enter().append("image");
        // update:
        image = entered.merge(image)
            .attr('xlink:href', function(d) {
                return 'http://' + 'abc' [d.y % 3] + '.tile.openstreetmap.org/' +
                    d.z + '/' + d.x + '/' + d.y + '.png';
            })
            .attr('x', function(d) {
                return d.x * 256;
            })
            .attr('y', function(d) {
                return d.y * 256;
            })
            .attr("width", 256)
            .attr("height", 256);
    }

    function stringify(scale, translate) {
        var k = scale / 256,
            r = scale % 1 ? Number : Math.round;
        return "translate(" + r(translate[0] * scale) + "," + r(translate[1] * scale) + ") scale(" + k + ")";
    }
</script>