【问题标题】:how to display data points from a csv file to a google map uning d3如何使用 d3 将 csv 文件中的数据点显示到谷歌地图
【发布时间】:2026-01-27 18:55:01
【问题描述】:

我正在尝试在我的响应式网站中使用 d3 在 Google 地图上的 csv 文件中显示一些数据点。谷歌地图按预期显示并且工作正常但是没有显示数据点有人可以帮忙吗? :(

   <script type="text/javascript">
        // Create the Google Map…           
        var map = new google.maps.Map(d3.select("#map").node(), {
            zoom: 7,
            center: new google.maps.LatLng(51.5000, 0.1167),
            mapTypeId: google.maps.MapTypeId.TERRAIN
        });

        // Load the station data. When the data comes back, create an overlay.
        d3.json("stations.json", function(data) {
          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().overlayLayer).append("div")
                .attr("class", "stations");

            // Draw each marker as a separate SVG element.
            // We could use a single SVG, but what size would it have?
            overlay.draw = function() {
              var projection = this.getProjection(),
                  padding = 10;

              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("svg:circle")
                  .attr("r", 4.5)
                  .attr("cx", padding)
                  .attr("cy", padding);

              // Add a label.
              marker.append("svg:text")
                  .attr("x", padding + 7)
                  .attr("y", padding)
                  .attr("dy", ".31em")
                  .text(function(d) { return d.key; });

              function transform(d) {
                d = new google.maps.LatLng(d.value[1], d.value[0]);
                d = projection.fromLatLngToDivPixel(d);
                return d3.select(this)
                    .style("left", (d.x - padding) + "px")
                    .style("top", (d.y - padding) + "px");  
              }
            };
          };

          // Bind our overlay to the map…
          overlay.setMap(map);    

          // Plot data points from .csv file
        d3.csv("data/data.csv", function(csv) {

            d3.selectAll("circle")
                .data(csv)
                .enter()
                .append("circle")
                .attr("cx", function(d) {
                    return projection([d.lat, d.lon])[0];
                })
                .attr("cy", function(d) {
                    return projection([d.lat, d.lon])[1];
                })
                .attr("r", 5)
                .style("fill", "red")
                .style("opacity", 0.90);            
        });
        });

    </script>`

【问题讨论】:

    标签: javascript google-maps csv d3.js


    【解决方案1】:

    这个脚本过去曾为我工作过......

    // Create the Google Map…
    var map = new google.maps.Map(d3.select("#map").node(), {
      zoom: 6,
      center: new google.maps.LatLng(29.6520, -82.3250),
      mapTypeId: google.maps.MapTypeId.ROADMAP
    });
    
    var data = {% raw said %};
    
    // Load the station data. When the data comes back, create an overlay.
      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().overlayLayer).append("div")
            .attr("class", "stations");
    
        // Draw each marker as a separate SVG element.
        // We could use a single SVG, but what size would it have?
        overlay.draw = function() {
          var projection = this.getProjection(),
              padding = 30;
    
        var marker = layer.selectAll("svg")
          .data(d3.entries(data))
          .each(transform) // update existing markers
          .enter().append("svg:svg")
          .each(transform)
          .attr("class", "marker");
    
    
        //Create color Scale
        var cScale = d3.scale.linear()
                       .domain([0, d3.max(d3.entries(data), function(d) { return d.value['Status_count']; })])
                       .range(["orange","yellow"]);         
    
        //Create radius Scale
        var rScale = d3.scale.linear()
                       .domain([0, d3.max(d3.entries(data), function(d) { return d.value['Status_count']; })])
                       .range([5, 15]);         
    
        // Add a circle.
        marker.append("svg:circle")
          .transition()
          .delay(1000)
          .attr("r", function(d) {return rScale(d.value['Status_count']);})           
          .attr("cx", padding)
          .attr("cy", padding)
          .attr('opacity', 0.75)
          .attr("fill", function(d) {return cScale(d.value['Status_count']);});
    
    
        // Add a label.
        marker.append("svg:text")
         .transition()
         .delay(1000)
         .attr("x", padding + 1)
         .attr("y", padding)
         .attr("dy", ".31em")            
         .text(function(d) { return d.value['Status_count']; });
    
        function transform(d) {
        d = new google.maps.LatLng(d.value['LAT'], d.value['LON']);
        d = projection.fromLatLngToDivPixel(d);
        return d3.select(this)
            .style("left", (d.x - padding) + "px")
            .style("top", (d.y - padding) + "px");
        }
    
    
        }; // overlay.draw
        }; // overlay.onAdd
    
    // Bind our overlay to the map…
    overlay.setMap(map);
    

    【讨论】:

    • 感谢DataByDavid,但是地图现在根本没有加载上述答案,或者我可能做错了什么。问题是我需要找到一种方法,从我存储在我的 PC 上的 csv 文件中将数据点绘制到我现有的地图上。
    • 确保替换这一行..."var data = {% raw said %};"。你用的是什么服务器?
    • 我已经更改了您在下面的答案中指定的行,但是,事情似乎是一样的。非常感谢您在这里提供的任何帮助,非常感谢
    • 我要绘制的数据点在我的电脑上而不是服务器上
    • 把代码和数据文件发给我,周末后我会搞砸的...... pichonz@gmail.com
    最近更新 更多