score:2

Accepted answer

I'd make a slight deviation in your approach. Instead of appending an svg per station, I'd append a single svg (if you need to draw lines from station to station you'll have to do this) and then append a g per station. The lines then are an easy loop over your links, calculate position and add the lines:

Here's the 3 main positioning functions then:

  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');

Full code:

<!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
    });

    //d3.json("stations.json", function(error, data) {

    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": 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>


Related Query

More Query from same tag