Drag-and-Drop Image Vector

 

Example of using the drag-and-drop interaction with image vector rendering.

Example of using the drag-and-drop interaction with an ol/layer/Vector with renderMode: 'image'. Drag and drop GPX, GeoJSON, IGC, KML, or TopoJSON files on to the map. Each file is rendered to an image on the client.

<!DOCTYPE html>
<html>
  <head>
    <title>Drag-and-Drop Image Vector</title>
    <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/css/ol.css" type="text/css">
    <!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
    <script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>

  </head>
  <body>
    <div id="map" class="map"></div>
    <div id="info">&nbsp;</div>
    <script>
      import Map from 'ol/Map.js';
      import View from 'ol/View.js';
      import {GPX, GeoJSON, IGC, KML, TopoJSON} from 'ol/format.js';
      import {defaults as defaultInteractions, DragAndDrop} from 'ol/interaction.js';
      import {Vector as VectorLayer, Tile as TileLayer} from 'ol/layer.js';
      import {BingMaps, Vector as VectorSource} from 'ol/source.js';

      var dragAndDropInteraction = new DragAndDrop({
        formatConstructors: [
          GPX,
          GeoJSON,
          IGC,
          KML,
          TopoJSON
        ]
      });

      var map = new Map({
        interactions: defaultInteractions().extend([dragAndDropInteraction]),
        layers: [
          new TileLayer({
            source: new BingMaps({
              imagerySet: 'Aerial',
              key: 'Your Bing Maps Key from http://www.bingmapsportal.com/ here'
            })
          })
        ],
        target: 'map',
        view: new View({
          center: [0, 0],
          zoom: 2
        })
      });

      dragAndDropInteraction.on('addfeatures', function(event) {
        var vectorSource = new VectorSource({
          features: event.features
        });
        map.addLayer(new VectorLayer({
          renderMode: 'image',
          source: vectorSource
        }));
        map.getView().fit(vectorSource.getExtent());
      });

      var displayFeatureInfo = function(pixel) {
        var features = [];
        map.forEachFeatureAtPixel(pixel, function(feature) {
          features.push(feature);
        });
        if (features.length > 0) {
          var info = [];
          var i, ii;
          for (i = 0, ii = features.length; i < ii; ++i) {
            info.push(features[i].get('name'));
          }
          document.getElementById('info').innerHTML = info.join(', ') || '&nbsp';
        } else {
          document.getElementById('info').innerHTML = '&nbsp;';
        }
      };

      map.on('pointermove', function(evt) {
        if (evt.dragging) {
          return;
        }
        var pixel = map.getEventPixel(evt.originalEvent);
        displayFeatureInfo(pixel);
      });

      map.on('click', function(evt) {
        displayFeatureInfo(evt.pixel);
      });
    </script>
  </body>
</html>