1 2 3 4 5 6 7 8 9 10 11 12 13 14 15 16 17 18 19 20 21 22 23 24 25 26 27 28 29 30 31 32 33 34 35 36 37 38 39 40 41 42 43 44 45 46 47 48 49 50 51 52 53 54 55 56 57 58 59 60 61 62 63 64 65 66 67 68 69 70 71 72 73 74 75 76 77 78 79 80 81 82 83 84 85 86 87 88 89 90 91 92 93 94 95 96 97 98 99 100 101 102 103 104 105 106 107 108 109 110 111 112 113 114 115 116 117 118 119 120 121 122 123 124 125 126 127 128 129 130 131 132 133 134 135 136 137 138 139 140 141 142 143 144 145 146 147 148 149 150 151 152 153 154 155 156 157 158 159 160 161 162 163 164 165 166
| import Feature from 'ol/Feature.js'; import Map from 'ol/Map.js'; import View from 'ol/View.js'; import {LineString, Point, Polygon} from 'ol/geom.js'; import { Pointer as PointerInteraction, defaults as defaultInteractions, } from 'ol/interaction.js'; import {TileJSON, Vector as VectorSource} from 'ol/source.js'; import {Tile as TileLayer, Vector as VectorLayer} from 'ol/layer.js';
class Drag extends PointerInteraction { constructor() { super({ handleDownEvent: handleDownEvent, handleDragEvent: handleDragEvent, handleMoveEvent: handleMoveEvent, handleUpEvent: handleUpEvent, });
this.coordinate_ = null;
this.cursor_ = 'pointer';
this.feature_ = null;
this.previousCursor_ = undefined; } }
function handleDownEvent(evt) { const map = evt.map;
const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { return feature; });
if (feature) { this.coordinate_ = evt.coordinate; this.feature_ = feature; }
return !!feature; }
function handleDragEvent(evt) { const deltaX = evt.coordinate[0] - this.coordinate_[0]; const deltaY = evt.coordinate[1] - this.coordinate_[1];
const geometry = this.feature_.getGeometry(); geometry.translate(deltaX, deltaY);
this.coordinate_[0] = evt.coordinate[0]; this.coordinate_[1] = evt.coordinate[1]; }
function handleMoveEvent(evt) { if (this.cursor_) { const map = evt.map; const feature = map.forEachFeatureAtPixel(evt.pixel, function (feature) { return feature; }); const element = evt.map.getTargetElement(); if (feature) { if (element.style.cursor != this.cursor_) { this.previousCursor_ = element.style.cursor; element.style.cursor = this.cursor_; } } else if (this.previousCursor_ !== undefined) { element.style.cursor = this.previousCursor_; this.previousCursor_ = undefined; } } }
function handleUpEvent() { this.coordinate_ = null; this.feature_ = null; return false; }
const pointFeature = new Feature(new Point([0, 0]));
const lineFeature = new Feature( new LineString([ [-1e7, 1e6], [-1e6, 3e6], ]) );
const polygonFeature = new Feature( new Polygon([ [ [-3e6, -1e6], [-3e6, 1e6], [-1e6, 1e6], [-1e6, -1e6], [-3e6, -1e6], ], ]) );
const key = 'Your Mapbox access token from https://mapbox.com/ here';
const map = new Map({ interactions: defaultInteractions().extend([new Drag()]), layers: [ new TileLayer({ source: new TileJSON({ url: 'https://a.tiles.mapbox.com/v4/aj.1x1-degrees.json?secure&access_token=' + key, }), }), new VectorLayer({ source: new VectorSource({ features: [pointFeature, lineFeature, polygonFeature], }), style: { 'icon-src': 'data/icon.png', 'icon-opacity': 0.95, 'icon-anchor': [0.5, 46], 'icon-anchor-x-units': 'fraction', 'icon-anchor-y-units': 'pixels', 'stroke-width': 3, 'stroke-color': [255, 0, 0, 1], 'fill-color': [0, 0, 255, 0.6], }, }), ], target: 'map', view: new View({ center: [0, 0], zoom: 2, }), });
|