Translate Features

This example uses OpenLayers v4.0.1. The latest is v.

#### Translate Features

Example of a translate features interaction.

This example demonstrates how the translate and select interactions can be used together. Zoom in to an area of interest and click to select a feature. Then drag the feature around to move it elsewhere on the map.

Related API documentation:

- ol.Map
,
- ol.View
,
- ol.format.GeoJSON
,
- ol.interaction
,
- ol.interaction.Select
,
- ol.interaction.Translate
,
- ol.layer.Tile
,
- ol.layer.Vector
,
- ol.source.OSM
,
- ol.source.Vector

Copy Edit
Translate Features Example of a translate features interaction. var raster = new ol.layer.Tile({ source: new ol.source.OSM() }); var vector = new ol.layer.Vector({ source: new ol.source.Vector({ url: 'https://openlayers.org/en/v4.0.1/examples/data/geojson/countries.geojson', format: new ol.format.GeoJSON() }) }); var select = new ol.interaction.Select(); var translate = new ol.interaction.Translate({ features: select.getFeatures() }); var map = new ol.Map({ interactions: ol.interaction.defaults().extend([select, translate]), layers: [raster, vector], target: 'map', view: new ol.View({ center: [0, 0], zoom: 2 }) }); <div id="map" class="map"></div>

    <!DOCTYPE html>
    <html>
      <head>
        <title>Translate Features</title>
        <link rel="stylesheet" href="https://openlayers.org/en/v4.0.1/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>
        <script src="https://openlayers.org/en/v4.0.1/build/ol.js"></script>
      </head>
      <body>
        <div id="map" class="map"></div>
        <script>
          var raster = new ol.layer.Tile({
            source: new ol.source.OSM()
          });

          var vector = new ol.layer.Vector({
            source: new ol.source.Vector({
              url: 'https://openlayers.org/en/v4.0.1/examples/data/geojson/countries.geojson',
              format: new ol.format.GeoJSON()
            })
          });

          var select = new ol.interaction.Select();

          var translate = new ol.interaction.Translate({
            features: select.getFeatures()
          });

          var map = new ol.Map({
            interactions: ol.interaction.defaults().extend([select, translate]),
            layers: [raster, vector],
            target: 'map',
            view: new ol.View({
              center: [0, 0],
              zoom: 2
            })
          });
        </script>
      </body>
    </html>