Friday 17 March 2017

d3.json() undefined googleoverlay

I'm super new to coding and trying to implement the following code: https://bl.ocks.org/mbostock/899711. Instead of a local .json file, I am reading a json from a url, unfortunately the LAT and LONG are strings so I parse them using data.forEach() but when I call the parsed d.LAT and d.LONG outside data.forEach(), the d.LAT and d.LONG are being read but returning as undefined.

var map = new google.maps.Map(d3.select(".box-panel-map").node(), {
  zoom: 15,
  draggableCursor: 'crosshair',
  center: new google.maps.LatLng(54.52936037,-117.46484254),
  mapTypeId: google.maps.MapTypeId.TERRAIN,
  backgroundColor: "white",
  mapMaker: 'True',
  styles: [
  {
    featureType: "all",
    elementType: "labels",
    stylers: [{ visibility: "off" }]
  }
  ]
});


var url ="";

d3.json(url, function(data) {
        data.forEach(function(d) {
            d.LAT = +d.LAT___DEG; 
            d.LONG = +d.LONG___DEG;
        });

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

  // Add the container when the overlay is added to the map.
  overlay.onAdd = function() {
    var layer = d3.select(this.getPanes().overlayMouseTarget).append("div")
      .attr("class", "pipemap");

    // Draw each marker as a separate SVG element.
    overlay.draw = function() {

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

      function transform (d) {
            console.log(d.LAT); //THIS IS RETURNING AS UNDEFINED
            var googleCoordinates = new google.maps.LatLng(d.LAT, d.LONG);
            var pos = projection.fromLatLngToDivPixel(googleCoordinates);
            return d3.select(this)
              .style("left", (pos.x - padding) + "px")
              .style("top", (pos.y - padding) + "px");
              //.attr('fill', color(d.value[2]))     
          }


      /*var color = d3.scale.linear()
        .domain([0, 1])
        .range(["blue", "red"]);   */
      var cValue = function(d) { return d.GFLAG;},
           color = d3.scale.linear()
            .domain([0, 1])
            .range(["blue", "red"]);  

      var tooltip = d3.select("body")
        .append("div")
        .attr("class", "tooltip")
        .style("opacity", 0);

      var marker = layer.selectAll("svg")
        .data(d3.entries(data))
        .each(transform) // update existing markers 
        .enter().append("svg:svg")
          .each(transform)
          .attr("class", "marker");

      // Add a circle.
      marker.append("circle")
        .attr("r", 2.5)
        .attr("cx", padding)
        .attr("cy", padding)
        .style("fill", function(d) { return color(cValue(d));}) 
        .on("mouseover", function(d) {
          tooltip.transition()
            .duration(200)
            .style("opacity", .9);
         // tooltip.html('Population: '+d.key+'<br>'+'Allele Frequencey: '+d.value[2].toPrecision(3))
          tooltip.html('Feature-Type: '+d.key+'')
            .style("left", (d3.event.pageX + 5) + "px")
            .style("top", (d3.event.pageY - 28) + "px");
        })
        .on("mouseout", function(d) {
          tooltip.transition()
          .duration(200)
          .style("opacity", 0);
          });


        };
      }; 
   overlay.setMap(map);
}); 

JSON object example inside the URL I'm reading:

    [{"FEATURE_NUMBER":"ABC-1115",
"LAT___DEG":"54.412530",
"LONG___DEG":"-117.447681",
"FLAG":0,"COMMENTS":null}]     



via Mariane

No comments:

Post a Comment