简体   繁体   中英

How to save geojson data from OSRM to a text file

I am using leaflet routine machine and mapbox to get routes. 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. I am using javascript with a browser, I can only see example in node.js, is this the only way?

Any ideas?

You can use FileSaver library to save files on the client-side

// 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

The technical post webpages of this site follow the CC BY-SA 4.0 protocol. If you need to reprint, please indicate the site URL or the original address.Any question please contact:yoyou2525@163.com.

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