简体   繁体   English

ReactJS-Leaflet:在 leaflet 中上传 csv 文件

[英]ReactJS-Leaflet: upload csv file in leaflet

here I have a question regarding references to creating a feature in the react leaflet.在这里,我有一个关于在反应 leaflet 中创建功能的参考的问题。

So, the feature has the following functions =因此,该功能具有以下功能 =

  1. The user page when they want to upload a location is in the form of a csv file containing latitude and longitude.当他们想要上传位置时,用户页面采用包含纬度和经度的 csv 文件的形式。

在此处输入图像描述

  1. When the user clicks the red button above, a popup will appear to upload the csv file.当用户点击上面的红色按钮时,会弹出一个上传csv文件的弹窗。

在此处输入图像描述

  1. When finished uploading the csv file, it will go directly to the location based on latitude and longitude.上传完csv文件后,会根据经纬度将go直接上传到位置。

在此处输入图像描述

So my question is, does anyone have a tutorial on how to create a csv upload button that points directly to a map with reactjs and leaflets?所以我的问题是,是否有人有关于如何创建 csv 上传按钮的教程,该按钮直接指向带有 reactjs 和传单的 map? Thank you very much非常感谢

Although you have not asked to use react-leaflet I would advise you to do so because you will end up in a mess when you will have to export the map reference to reuse it across places.尽管您没有要求使用 react-leaflet,但我建议您这样做,因为当您必须导出 map 引用以跨地方重用它时,您最终会陷入混乱。

First create a button that will handle the upload of a csv file.首先创建一个按钮来处理csv文件的上传。 There is a really useful guide to do so without the use of libraries like paparse although it simplifies a lot this procedure.有一个非常有用的指南可以在不使用像paparse这样的库的情况下这样做,尽管它大大简化了这个过程。 Next you need to transform the csv columns to some form of data to use.接下来,您需要将 csv 列转换为某种形式的数据以供使用。 This is also included in the guide.这也包含在指南中。 You end up with an array of csv columns.您最终会得到一组 csv 列。

Then all you need to do is to create a custom react-leaflet component to render the markers and zoom to the markers viewport.然后你需要做的就是创建一个自定义的 react-leaflet 组件来渲染标记并缩放到标记视口。

Also you can clean the csv file once you insert a new one.您也可以在插入新文件后清理 csv 文件。

function App() {
  const [csvToArray, setCsvToArray] = useState([]);

  const fileReader = new FileReader();

  const csvFileToArray = (string) => {
    const csvHeader = string.slice(0, string.indexOf("\n")).split(",");
    const csvRows = string.slice(string.indexOf("\n") + 1).split("\n");

    const array = csvRows.map((i) => {
      const values = i.split(",");
      const obj = csvHeader.reduce((object, header, index) => {
        object[header] = values[index];
        return object;
      }, {});
      return obj;
    });

    setCsvToArray(array);
  };

  const handleOnChange = (e) => {
    if (csvFileToArray.length > 0) setCsvToArray([]);

    const file = e.target.files[0];
    if (file) {
      fileReader.onload = function (event) {
        const text = event.target.result;
        csvFileToArray(text);
      };

      fileReader.readAsText(file);
    }
  };

  console.log(csvToArray);

  return (
    <>
      <input
        type="file"
        id="csvFileInput"
        accept=".csv"
        onChange={handleOnChange}
      />
      <Map csvToArray={csvToArray} />
    </>
  );
}

function RenderCsvToArray({ csvToArray }) {
  const map = useMap();

  useEffect(() => {
    if (csvToArray.length === 0) return;

    const myPoints = csvToArray.map(({ Latitude, Longitude }) => [
      Latitude,
      Longitude
    ]);
    const myBounds = new L.LatLngBounds(myPoints);
    map.fitBounds(myBounds);
  }, [csvToArray]);

  return csvToArray?.map(({ Latitude, Longitude, Title }, index) => (
    <Marker key={index} icon={icon} position={[Latitude, Longitude]}>
      <Popup>{Title}</Popup>
    </Marker>
  ));
}

You can see the full implementation on the demo您可以在演示中看到完整的实现

I have also inlcuded two csv files to play with in the form of我还包含了两个 csv 文件以使用的形式

Title,Latitude,Longitude
Trinity College,41.745167,-72.69263
Wesleyan University,41.55709,-72.65691

and

Group,Title,Image,Description,Address,Latitude,Longitude
a,Trinity College,https://www.edx.org/sites/default/files/trinity1.jpg,"Not in the link <a href=""http://www.trincoll.edu"" target=""_blank"">view website</a> more not in the link","300 Summit St - Hartford CT 06106,41.745167,-72.69263
a,Wesleyan University,https://upload.wikimedia.org/wikipedia/commons/4/41/You_are_here_-_T-shirt.jpg,"<a href=""http://wesleyan.edu"" target=""_blank"">view website</a>",45 Wyllys Ave Middletown CT 06459,41.55709,-72.65691

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM