使用d3js在Google地图上的相同节点之间创建多个链接

时间:2017-02-16 06:07:32

标签: d3.js

我尝试使用d3js在Google地图上的相同节点之间创建多个链接。 但它显示单一链接。

在map下面,我试图在Node-0和No​​de-1之间创建两个链接。 但是我在这些节点之间的地图上只有一个链接。

代码:

<!DOCTYPE html>
<meta charset="utf-8">
<meta name="viewport" content="initial-scale=1.0, user-scalable=no" />
<html>

<head>
  <script src="http://maps.google.com/maps/api/js?sensor=false"></script>
  <script src="http://d3js.org/d3.v3.min.js"></script>

  <style>
    html,
    body,
    #map {
      width: 100%;
      height: 100%;
      margin: 0;
      padding: 0;
    }

    .link {
      stroke: #ccc;
      stroke-width: 1.5px;
    }

    .stations,
    .stations svg {
      position: absolute;
    }

    .stations svg {
      width: 1200px;
      height: 600px;
      padding-right: 100px;
      font: 12px sans-serif;
    }

    .stations circle {
      fill: brown;
      stroke: black;
      stroke-width: 1.5px;
    }
  </style>

</head>

<body>
  <div id="map" style="width: 1200px; height: 600px;"></div>
  <script type="text/javascript">
    // Create the Google Map…
    var map = new google.maps.Map(d3.select("#map").node(), {
      zoom: 12,
      center: new google.maps.LatLng(51.5195, -0.1269),
      mapTypeId: google.maps.MapTypeId.TERRAIN
    });



    var data = {
      "nodes": [{
        "name": "A",
        "long": 0.0000,
        "lat": 51.4800
      }, {
        "name": "B",
        "long": -0.1684,
        "lat": 51.4875
      }, {
        "name": "C",
        "long": -0.2043,
        "lat": 51.5096
      }, {
        "name": "D",
        "long": -0.1269,
        "lat": 51.5295
      }],
      "links": [{
        "source": 0,
        "target": 1
      }, {
        "source": 0,
        "target": 1
      },{
        "source": 0,
        "target": 2
      }, {
        "source": 0,
        "target": 3
      }, {
        "source": 2,
        "target": 3
      }]
    }

    var overlay = new google.maps.OverlayView();

    overlay.onAdd = function() {
      var layer = d3.select(this.getPanes().overlayLayer).append("div")
        .attr("class", "stations");

      overlay.draw = function() {

        layer.select('svg').remove();

        var projection = this.getProjection(),
          padding = 10;

        var svg = layer.append("svg")
          .attr('width', 1200)
          .attr('height', 600)

        var node = svg.selectAll(".stations")
          .data(data.nodes)
          .enter().append("g")
          .each(transform)
          .attr("class", "node");

        var link = svg.selectAll(".link")
          .data(data.links)
          .enter().append("line")
          .attr("class", "link")
          .each(drawlink);

        node.append("circle")
          .attr("r", 4.5);

        node.append("text")
          .attr("x", 7)
          .attr("y", 0)
          .attr("dy", ".31em")
          .text(function(d) {
            return d.name;
          });

        function latLongToPos(d) {
          var p = new google.maps.LatLng(d.lat, d.long);
          p = projection.fromLatLngToDivPixel(p);
          p.x = p.x - padding;
          p.y = p.y - padding;
          return p;
        }

        function transform(d) {
          var p = latLongToPos(d);
          return d3.select(this)
            .attr("transform", "translate(" + p.x + "," + p.y + ")");
        }

        function drawlink(d) {
          var p1 = latLongToPos(data.nodes[d.source]),
            p2 = latLongToPos(data.nodes[d.target]);
          d3.select(this)
            .attr('x1', p1.x)
            .attr('y1', p1.y)
            .attr('x2', p2.x)
            .attr('y2', p2.y)
            .style('fill', 'none')
            .style('stroke', 'steelblue');
        }
      };
    };

    overlay.setMap(map);

  </script>
</body>

</html>

0 个答案:

没有答案