You need to enable JavaScript to run this app.
优惠活动
大模型
产品
解决方案
定价
更多

在D3可视化中使用Google Maps HeatmapLayer遇问题求助

Fixing the "Cannot read property 'NaN' of undefined" Error with Google Maps HeatmapLayer in D3

Hey there, let's break down what's causing that frustrating error and how to fix it. That Uncaught TypeError almost always boils down to incorrect data formatting for the Google Maps HeatmapLayer, or trying to initialize the layer before your data/map is ready.

Why This Happens

Google Maps' HeatmapLayer expects a specific data structure: an array of objects with either a location property (a google.maps.LatLng instance) or lat/lng numeric values, plus an optional weight for intensity. When you pull data from a CSV via D3, the lat/lon values are strings by default—if you don't convert them to numbers, you'll end up with NaN values, which breaks the heatmap's internal logic and throws that confusing error.

Step-by-Step Fix

1. Properly Parse Your CSV Data

First, make sure you're converting the CSV's string values to numbers and structuring them to match what the heatmap expects:

d3.csv("your-data.csv", function(data) {
  // Convert CSV strings to numbers and format for heatmap
  const heatmapData = data.map(d => ({
    location: new google.maps.LatLng(+d.lat, +d.lon),
    weight: +d.temp1 // Use temp1 or temp2 as your intensity value
  }));

  // Now use heatmapData for your layer
});

The + prefix converts the string values from your CSV into numbers—without this, d.lat and d.lon will remain strings, leading to invalid NaN values when creating the LatLng object.

2. Ensure Your Map Instance Is Accessible

Double-check that you're passing the correct Google Maps instance to the HeatmapLayer. If you're using Mike Bostock's D3 + Google Maps setup, you need to grab the underlying google.maps.Map object (not just the D3-wrapped layer):

// Assuming you have a D3 map layer set up like this:
const width = 800;
const height = 600;

const map = d3.select("#map")
  .append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .call(d3.geo.google()
    .center([-87.6298, 41.8781]) // Example: Chicago coordinates
    .zoom(10)
    .size([width, height]));

// Grab the actual Google Maps instance from the D3 layer
const googleMap = map.node().__googleMap;

// Initialize the heatmap with the correct map reference
const heatmap = new google.maps.visualization.HeatmapLayer({
  data: heatmapData,
  map: googleMap
});

If you don't reference the real Google Maps instance, the heatmap won't attach properly, which can also throw weird undefined errors.

3. Clean Up Bad Data

Scan your CSV for empty or malformed lat/lon values. Even one row with a missing lat or lon will create a NaN LatLng, which can trigger that error. Add a quick filter to clean your data:

const heatmapData = data
  .filter(d => d.lat && d.lon && !isNaN(+d.lat) && !isNaN(+d.lon))
  .map(d => ({
    location: new google.maps.LatLng(+d.lat, +d.lon),
    weight: +d.temp1
  }));

Example Full Code Snippet

Here's how it all fits together in a working setup:

// Set up D3 + Google Maps container
const width = 800;
const height = 600;

const map = d3.select("#map")
  .append("svg")
  .attr("width", width)
  .attr("height", height)
  .append("g")
  .call(d3.geo.google()
    .center([-87.6298, 41.8781])
    .zoom(10)
    .size([width, height]));

const googleMap = map.node().__googleMap;

// Load and process CSV data
d3.csv("your-data.csv", function(error, data) {
  if (error) throw error;

  const heatmapData = data
    .filter(d => d.lat && d.lon && !isNaN(+d.lat) && !isNaN(+d.lon))
    .map(d => ({
      location: new google.maps.LatLng(+d.lat, +d.lon),
      weight: +d.temp1
    }));

  // Add heatmap layer to the map
  const heatmap = new google.maps.visualization.HeatmapLayer({
    data: heatmapData,
    map: googleMap,
    radius: 20 // Adjust this to control the heatpoint spread
  });
});

Quick Troubleshooting Tips

  • Wait for Google Maps to load: If you're initializing the heatmap before the Google Maps API is fully loaded, you'll get undefined errors. Wrap your heatmap code in an API load callback (use the callback parameter in your Google Maps script tag with async defer).
  • Log your data: Before passing heatmapData to the layer, log it to the console to confirm all location values are valid LatLng objects (not NaN).

内容的提问来源于stack exchange,提问作者Bharat K

相关产品推荐
方舟 Agent Plan

超全模态模型 × Harness 升级,最新支持 Deepseek-V4.1-Flash、GLM-5.3 系列、Doubao-Seedream-5.0-pro、Kimi-K3 (部分), 限时 9.9 元起

最近更新时间:2026.05.25 04:27:42