无法使d3地图正确居中

时间:2015-08-18 07:53:28

标签: d3.js d3.geo

我有一张在几个地点之间来回缩放的地图。但是,我的地图坐标已关闭。默认情况下,地图显示为[0,0]。我将projection.center更改为一个新点,但这引起了一些混乱 - (地图开始缩放到其他大陆......)

带地图的

Here is a plunker,0,0中心(鼠标悬停在底部,你会看到坐标)。

我也尝试在zoomTo函数中设置projection.center,但这引起了另一个问题。我觉得这应该是一个快速编辑,但我花了很多时间把头发拉出来。

关于需要改变什么的任何想法?

非常感谢。

代码也在下面:

    <!DOCTYPE html>
    <meta charset="utf-8">
    <style>

    body {
      margin: 0;
    }

    #container {
      position: relative;
      overflow: hidden;
    }

    #map{
        width:100%;
        height:100%;
    }

    .layer {
      position: absolute;
    }

    .tile {
      pointer-events: none;
      position: absolute;
      width: 256px;
      height: 256px;
    }

    .info {
      position: absolute;
      bottom: 0px;
      left: 0px;
        padding: 20px;
        background: #000;
        color: #fff;
        width: 100%;
        z-index: 1000;
    }
    </style>
    <body>
    <script src="http://d3js.org/d3.v3.min.js"></script>
    <script src="http://d3js.org/d3.geo.tile.v0.min.js"></script>
    <div id="canvas">
        <div id="container">
            <div id="map">
                <div class="layer"></div>
            </div>
        </div>
    </div>
    <script>

    var width = Math.max(960, window.innerWidth),
        height = Math.max(500, window.innerHeight),
        prefix = prefixMatch(["webkit", "ms", "Moz", "O"]);

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

    var sf = [-122.417, 37.775],
            belowsf = [-122.510962, 37.580284];

    var projection = d3.geo.mercator()
            .scale((1 << 18) / 2 / Math.PI)
            .translate([-width / 2, -height / 2]); // just temporary

    var map = d3.select("#map");

    var layer = d3.select(".layer");

    var zoom = d3.behavior.zoom().on("zoom", zoomed);

    var canvas = d3.select("#canvas")
            .style("width", width + "px")
            .style("height", height + "px");

    var container = d3.select("#container")
        .style("width", width + "px")
        .style("height", height + "px")
        .on("mousemove", mousemoved);

            canvas  
                    .call(zoomTo(sf).event)
                        .transition()
                        .duration(10000)
                        .each(jump);

    var info = map.append("div")
            .attr("class", "info");

    function zoomTo(place) {
            return zoom
                        .scale(projection.scale() * 2 * Math.PI)
                    .translate(projection(place).map(function(x) { return -x; }));


    }

    function jump() {
      var t = d3.select(this);
                (function repeat() {
              t = t.transition()
              .call(zoomTo(belowsf).event)
              .transition()
              .call(zoomTo(sf).event)
              .each("end", repeat);
      })();
    }


    function mousemoved() {
      info.text(formatLocation(projection.invert(d3.mouse(this)), zoom.scale()));
    }

    function zoomed() {
      var tiles = tile
          .scale(zoom.scale())
          .translate(zoom.translate())
          ();

      var image = layer
          .style(prefix + "transform", matrix3d(tiles.scale, tiles.translate))
        .selectAll(".tile")
          .data(tiles, function(d) { return d; });

      image.exit()
          .remove();

      image.enter().append("img")
          .attr("class", "tile")
          .attr("src", function(d) { return "http://" + ["a", "b", "c"][Math.random() * 3 | 0] + ".basemaps.cartocdn.com/light_all/" + d[2] + "/" + d[0] + "/" + d[1] + ".png"; })
          .style("left", function(d) { return (d[0] << 8) + "px"; })
          .style("top", function(d) { return (d[1] << 8) + "px"; });
    }


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

    function prefixMatch(p) {
      var i = -1, n = p.length, s = document.body.style;
      while (++i < n) if (p[i] + "Transform" in s) return "-" + p[i].toLowerCase() + "-";
      return "";
    }

    function formatLocation(p, k) {
      var format = d3.format("." + Math.floor(Math.log(k) / 2 - 2) + "f");
      return (p[1] < 0 ? format(-p[1]) + "°S" : format(p[1]) + "°N") + " "
           + (p[0] < 0 ? format(-p[0]) + "°W" : format(p[0]) + "°E");
    }

    </script>
    </body>
    </html>

截图,显示不正确的坐标: enter image description here

1 个答案:

答案 0 :(得分:3)

好的,我想我明白了。 关于居中地图的第一个问题,您可以使用旧金山坐标投影来平移缩放:

var projection = d3.geo.mercator()
        .scale((1 << 18) / 2 / Math.PI)
        .translate([width / 2, height / 2]);    

var center = projection(sf);

var zoom = d3.behavior.zoom()
  .scale(projection.scale() * 2 * Math.PI)
  .translate([width - center[0], height - center[1]]);

设置好中心后,您需要更新投影以与缩放对齐,以便在屏幕上显示正确的坐标:

projection
     .scale(zoom.scale() / 2 / Math.PI)
     .translate(zoom.translate());

这是我在地图加载时所得到的,在我看来你正在寻找的坐标:

enter image description here

我还修改了ZoomTo功能,以便在转换时考虑上述变化:

function zoomTo(place) {
      // project back to main projection
      projection = d3.geo.mercator()
        .scale((1 << 18) / 2 / Math.PI)
        .translate([width / 2, height / 2])
      // set new center
      center = projection(place);
      // set zoom with new center
      return zoom
        .scale(projection.scale() * 2 * Math.PI)
        .translate([width - center[0], height - center[1]]);



    }

完整代码:http://plnkr.co/edit/eE0XZQPHB6rAc9cg4LMH?p=preview