【问题标题】:read data from csv file in javascript用javascript从csv文件中读取数据
【发布时间】:2017-07-10 18:19:19
【问题描述】:

我正在尝试通过 csv 数据在地图上放置点。 在csv中,我有纬度和经度字段... 我怎样才能做到这一点? 也许从csv中获取一个数组并使用它? 我尝试了很多东西,没有任何效果..

这是我的代码:

<!DOCTYPE HTML>
<html>
<head>
    <script src="http://www.webglearth.com/v2/api.js"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>

    <script>
        function initialize() {
            var earth = new WE.map('earth_div');
            WE.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(earth);
   
            //get the csv file data, create new marker [latitude,longitude]
            //the bind popup will get the field country

        for (var i = 0; i < 5; i++)
        {
            var marker = WE.marker([i,i]).addTo(earth);
            marker.bindPopup("<b>The data </b><br><a target=_blank href='http://www.google.com'>link</a>.<br />"
                , { maxWidth: 150, closeButton: true }).closePopup();
        }//end for i

        var markerCustom = WE.marker([50, -9], '/img/logo-webglearth-white-100.png', 100, 24).addTo(earth);

        earth.setView([0, 0], 3);
      }
    </script>
    <style>
        html, body {
            padding: 0;
            margin: 0;
            background-color: black;
        }

        #earth_div {
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            position: absolute !important;
        }
    </style>
    <title>WebGL Earth API: Markers</title>
</head>
<body onload="initialize()">
    <div id="earth_div"></div>
</body>
</html>

【问题讨论】:

  • 我认为你必须制作一个按特殊数字排序的数组,用于地图中的线协调并将它们调用到你的地图中
  • 你能帮我写一个代码吗?我不知道怎么...
  • link你可以看看这个。
  • 我看到了..但对我没有任何帮助..
  • d3.csv("mycsv.csv", function (d){ d['latitude'] = +d['latitude']; var marker = WE.marker([d['latitude '], 66]).addTo(earth); marker.bindPopup("数据
    google.com'>link</a>.<br />" , { maxWidth: 150, closeButton : true }).closePopup(); return d; });此代码不起作用..

标签: javascript csv d3.js


【解决方案1】:

这对你有用吗? 使用按钮选择 csv 文件,脚本将在页面上显示它。

index.html:

<!DOCTYPE HTML>
<html>
<head>
    <script src="http://www.webglearth.com/v2/api.js"></script>
    <script src="https://d3js.org/d3.v4.min.js"></script>
    <style>
        html, body {
            padding: 0;
            margin: 0;
            background-color: black;
        }

        #earth_div {
            top: 0;
            right: 0;
            bottom: 0;
            left: 0;
            position: absolute !important;
        }
        #files {
            top: 0;
            left: 0;
            position: absolute;
            z-index: 999;
        }
    </style>
    <title>WebGL Earth API: Markers</title>
</head>
<body>
    <input type="file" id="files" name="file" />
    <div id="earth_div"></div>
    <script>
        var earth = new WE.map('earth_div');
        WE.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png').addTo(earth);
        var markerCustom = WE.marker([50, -9], '/img/logo-webglearth-white-100.png', 100, 24).addTo(earth);
        earth.setView([0, 0], 3);
        document.getElementById('files').onchange = function() {
            readFile();
        };
        function readFile() {
            var files = document.getElementById('files').files;
            if (!files.length) {
              alert('Please select a file!');
              return;
            }
            var file = files[0];
            var start = 0;
            var stop = file.size - 1;
            var reader = new FileReader();
            reader.onloadend = function(evt) {
                if (evt.target.readyState == FileReader.DONE) {
                    var text = evt.target.result;
                    var lines = text.split('\n');
                    for (var i = 0; i < lines.length; i++) {
                        lines[i] = lines[i].split(',');
                        lines[i].map(function(val) {
                            return Number(val);
                        });
                    }
                    for (var i = 0; i < lines.length; i++) {
                        var marker = WE.marker([lines[i][0], lines[i][1]]).addTo(earth);
                        marker.bindPopup("<b>The data </b><br><a target=_blank href='http://www.google.com'>link</a>.<br />"
                            , { maxWidth: 150, closeButton: true }).closePopup();
                    }
                }
            }
            var blob = file.slice(start, stop + 1);
            reader.readAsBinaryString(blob);
        }
    </script>
</body>
</html>

points.csv:

1,1
2,2
3,3
4,4

【讨论】:

  • 是的!谢谢!现在,我可以在没有文件选择器的情况下加载我的文件吗?我的文件名是“mycsv2.csv”,我从中得到纬度和经度
  • 您可以向文件发出 ajax 请求并显示它。
  • 我知道如何在 csv 中调用所需的 cols。但我怎样才能自动加载文件?我不知道如何使用 ajax..
  • var xhttp = new XMLHttpRequest(); xhttp.onreadystatechange = function() { if (this.readyState == 4 && this.status == 200) { console.log(this.responseText); } }; xhttp.open("GET", "FILE", true); xhttp.send();
  • 如果您将文件托管在您的机器上,它将无法正常工作。
猜你喜欢
  • 2011-11-17
  • 1970-01-01
  • 2014-01-07
  • 2019-09-17
  • 1970-01-01
  • 2019-05-03
相关资源
最近更新 更多