简体   繁体   English

如何将geojson数据从OSRM保存到文本文件

[英]How to save geojson data from OSRM to a text file

I am using leaflet routine machine and mapbox to get routes. 我正在使用传单常规计算机和mapbox来获取路线。 All works fine and I can console.log the route out but ideally I would like to save the geojson data to a text file so I can do testing without calling the API every time and I can change things too. 一切正常,我可以console.log退出路由,但是理想情况下,我想将geojson数据保存到文本文件中,这样我就可以进行测试而无需每次都调用API,并且也可以进行更改。 I am using javascript with a browser, I can only see example in node.js, is this the only way? 我在浏览器中使用javascript,只能在node.js中看到示例,这是唯一的方法吗?

Any ideas? 有任何想法吗?

You can use FileSaver library to save files on the client-side 您可以使用FileSaver库将文件保存在客户端

// add the geojson to the map
const geoJson = L.geoJson(freeBus).addTo(map);

// use external library to save geojson
const saveTxt = (content, filename) => {
  const file = filename + ".json";
  saveAs(new File([JSON.stringify(content)], file, {
      type: "text/plain;charset=utf-8"
    }), file);
};

// invoke the function by passing geojson to be saved
// and .txt file name
saveTxt(geoJson.toGeoJSON(), "test");

Demo 演示版

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

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