javascript - 带有来自 CSV 的线层的传单 map

标签 javascript csv leaflet

我正在学习 javascript、leaflet 和 html。我正在尝试根据 CSV 文件中的坐标在 Leaflet map 上添加一条线。我认为这会非常简单,但几个小时后我没有运气,也没有在网上找到任何有用的例子。这是我的 HTML:

<!DOCTYPE html>
<html>
<head>
<meta charset=utf-8 />
<title>Loading a line from CSV</title>
<meta name='viewport' content='initial-scale=1,maximum-scale=1,user-scalable=no' />
<script src='https://api.mapbox.com/mapbox.js/v3.1.0/mapbox.js'></script>
<link href='https://api.mapbox.com/mapbox.js/v3.1.0/mapbox.css' rel='stylesheet' />
<style>
  body { margin:0; padding:0; }
  #map { position:absolute; top:0; bottom:0; width:100%; }
</style>
</head>
<body>
<script src='https://api.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.2.0/leaflet-omnivore.min.js'></script>
<script src='https://npmcdn.com/csv2geojson@latest/csv2geojson.js'></script>
<script src='D:/java/map_sample/js/papaparse.js'></script>
<div id='map'></div>

<script>
L.mapbox.accessToken = 'pk.eyJ1IjoiZWRhaWxleSIsImEiOiJjajJxZG56bXcwMnl6Mndxd2p6bnR6ajJ2In0.es2KsQs3aX_f9hUuQlPmyg';
var map = L.mapbox.map('map', 'mapbox.streets').setView([34.01, -120.27], 12);

var geoj = csv2geojson.toLine('https://raw.githubusercontent.com/evantdailey/map_testing/master/site1.csv', {
    latfield: 'Latitude',
    lonfield: 'Longitude'
});

L.geoJSON(geoj).addTo(map);

</script>  

当我运行它时,出现以下错误:

csv2geojson.js:192 Uncaught TypeError: Cannot read property 'length' of undefined
at Object.toLine (csv2geojson.js:192)
at leaflet_geoJSONLayer2.html:24  

任何人都可以告诉我我做错了什么,或者指出正确的方向吗? CSV 文件应该可以在 github 上访问

最佳答案

库不会为您发出 AJAX 请求,因此您正在尝试解析文字字符串 https://raw.githubusercontent.com/evantdailey/map_testing/master/site1.csv

您还尝试在请求 GeoJSON 对象时使用 CSV 字符串作为参数调用 toLine

这是一个非常基本的示例用法:

var geoj = csv2geojson.csv2geojson(`lat,lon,name
41.8781,-87.6298,3`, {
    latfield: 'lat',
    lonfield: 'lon'
}, function(err, data) {
  console.log(data);
});

关于javascript - 带有来自 CSV 的线层的传单 map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/43988504/

相关文章:

javascript - "Stop running this script?"错误

javascript - 错误 : How to serialize data from getStaticProps : Next. js

typescript - 扩展现有的@types 类型定义以包含缺失的属性

javascript - (null != someVariable) 或 (someVariable != null)

javascript - 如何像 Pinterest 一样呈现 "Continue as X"Facebook 登录

python - python 中的数据帧操作

csv - Spark SQL如何读取压缩的csv文件?

python - 找不到文件错误 : [Errno 2] when packaging for PyPI

javascript - 传单未将平铺层添加到底部

javascript - heatmap.js 中消失的数据点(传单插件)