Use HTML5 File Reader and send it to Leaflet Omnivore - Javascript Leaflet

Javascript examples for Leaflet:Configuration

Description

Use HTML5 File Reader and send it to Leaflet Omnivore

Demo Code

ResultView the demo in separate window

<html>
   <head> 
      <meta name="viewport" content="width=device-width, initial-scale=1"> 
      <link rel="stylesheet" type="text/css" href="https://unpkg.com/leaflet@1.1.0/dist/leaflet.css"> 
      <script type="text/javascript" src="https://unpkg.com/leaflet@1.1.0/dist/leaflet-src.js"></script> 
      <script type="text/javascript" src="https://api.tiles.mapbox.com/mapbox.js/plugins/leaflet-omnivore/v0.3.1/leaflet-omnivore.js"></script> 
      <style id="compiled-css" type="text/css">

#map {//w ww  . ja v  a 2 s .  c o  m
   height: 500px;
}


      </style> 
      <script type="text/javascript">
    window.onload=function(){
var map = L.map("map");
L.tileLayer("https://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png").addTo(map);
map.setView([48.85, 2.35], 12);
var fileInput = document.getElementById("fileInput");
fileInput.addEventListener('change', function(event) {
  var file = fileInput.files[0],
    fr = new FileReader();
  fileInput.value = ''; // Clear the input.
  fr.onload = function() {
    var layer = omnivore.csv.parse(fr.result).addTo(map); 
    map.fitBounds(layer.getBounds());
  };
  fr.readAsText(file);
});
    }

      </script> 
   </head> 
   <body> 
      <div id="map"></div> 
      <p>
          Load Points Of Interest from a CSV file: 
         <input type="file" id="fileInput"> 
      </p>  
   </body>
</html>

Related Tutorials