为什么 tableRow.get() 在 p5.js 中不起作用?

问题描述 投票:0回答:1

我正在尝试使用地理定位来练习数据表。但是p5无法从tableRow.get()加载数据,因为我打印了纬度和经度,它说未定义。有什么问题吗?

let Soho;
let table;
let lat, lon, xgeo, ygeo;

let mapGeoLeft = -0.1424;
let mapGeoRight = -0.131;
let mapGeoTop = 51.5164;
let mapGeoBottom = 51.5093;

function preload() {
  soho = loadImage("cholera.png");
  table = loadTable("cholera.csv");
}

function setup() {
  createCanvas(600, 600);
  background(soho);
}

function draw() {
  for (let r = 0; r < table.getRowCount(); r++) {
    let tableRow = table.rows[r];
    lat = tableRow.get("lat");
    lon = tableRow.get("lon");

    xgeo = map(lon, mapGeoLeft, mapGeoRight, 0, width);
    ygeo = map(lat, mapGeoTop, mapGeoBottom, 0, height);

    fill(255, 0, 0);
    strokeWeight(0.5);
    circle(xgeo, ygeo, 5);
  }
  print(lat);
  noLoop();
}

这是表格中的前几行数据:

count,lon,lat
3,-0.13793,51.513418
2,-0.137883,51.513361
1,-0.137853,51.513317
1,-0.137812,51.513262
4,-0.137767,51.513204
2,-0.137537,51.513184
javascript database p5.js
1个回答
0
投票

您可以使用第三个参数告诉表解析器有一个标头:

loadTable("cholera.csv", "csv", "header");

请参阅 文档了解

loadTable

顺便说一句,始终使用

const
let
而不是创建全局变量,因此
lat = tableRow.get("lat");
应该是
const lat = tableRow.get("lat");
,依此类推。

© www.soinside.com 2019 - 2024. All rights reserved.