Javascript Openlayers:TypeError:t.getType不是函数
我想在地图上拖放标记并显示位置。Javascript Openlayers:TypeError:t.getType不是函数,javascript,angular,typescript,openlayers,Javascript,Angular,Typescript,Openlayers,我想在地图上拖放标记并显示位置。 当我使用此代码拖放功能时: var modify = new Modify({ source: this.vectorSource }); his.map.addInteraction(modify); 发生以下错误: TypeError:t.getType不是函数 若你们需要在地图上移动一个要素,你们需要使用交互,但不需要修改。在openlayers中没有需要创建的移动交互。这是我的绘图 import PointerInteraction from 'ol/
当我使用此代码拖放功能时:
var modify = new Modify({ source: this.vectorSource });
his.map.addInteraction(modify);
发生以下错误:
TypeError:t.getType不是函数
若你们需要在地图上移动一个要素,你们需要使用交互,但不需要修改。在openlayers中没有需要创建的移动交互。这是我的绘图
import PointerInteraction from 'ol/interaction/Pointer';
import { MapBrowserEvent, Feature } from 'ol';
import Event from 'ol/events/Event';
type DragEventType = 'moveend';
export class DragInteractionEvent extends Event {
feature: Feature;
mapBrowserEvent: MapBrowserEvent;
constructor(type: DragEventType, feature: Feature, mapBrowserEvent: MapBrowserEvent) {
super(type);
this.feature = feature;
this.mapBrowserEvent = mapBrowserEvent;
}
}
// tslint:disable: only-arrow-functions
// tslint:disable: space-before-function-paren
export class DragInteraction extends PointerInteraction {
constructor(theFeature: Feature) {
super({
handleDownEvent(evt: MapBrowserEvent): boolean {
const map = evt.map;
const feature = map.forEachFeatureAtPixel(evt.pixel, function (f) {
return f;
});
if (feature && theFeature === feature) {
this.coordinate_ = evt.coordinate;
this.feature_ = feature;
return true;
}
return false
},
handleDragEvent(evt: MapBrowserEvent) {
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];
},
handleMoveEvent(evt: MapBrowserEvent) {
if (!this.cursor_) {
return;
}
const map = evt.map;
const feature = map.forEachFeatureAtPixel(evt.pixel, function (f) {
return f;
});
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;
}
},
handleUpEvent(evt: MapBrowserEvent): boolean {
this.dispatchEvent(new DragInteractionEvent('moveend', theFeature, evt))
this.coordinate_ = null;
this.feature_ = null;
return false;
},
})
}
}
你可以这样用
this.dragInteraction = new DragInteraction(feature);
this.dragInteraction.on('moveend', (evt: DragInteractionEvent) => {
console.log('evt', evt);
})
你可以查一下这个,我用它作为参考