Javascript 点击缩放按钮触发go Think all div并触发我的地图功能Anuglar10/OpenLayer6
我在OpenLayers和Angular中使用事件时遇到了一个问题,我在地图上有一个单击功能,从我在地图上单击的位置捕捉到了lonlat,但是,当我单击放大和缩小控件时,该功能也会在我拖动视图时触发,我不知道会发生什么。这是我第一次使用这个API,有人能帮我吗 我的TS文件Javascript 点击缩放按钮触发go Think all div并触发我的地图功能Anuglar10/OpenLayer6,javascript,angular,typescript,mouseevent,openlayers,Javascript,Angular,Typescript,Mouseevent,Openlayers,我在OpenLayers和Angular中使用事件时遇到了一个问题,我在地图上有一个单击功能,从我在地图上单击的位置捕捉到了lonlat,但是,当我单击放大和缩小控件时,该功能也会在我拖动视图时触发,我不知道会发生什么。这是我第一次使用这个API,有人能帮我吗 我的TS文件 import { Component, OnInit } from '@angular/core'; import Map from 'ol/Map'; import Tile from 'ol/layer/Tile'; i
import { Component, OnInit } from '@angular/core';
import Map from 'ol/Map';
import Tile from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';
import View from 'ol/View';
import { fromLonLat, transform } from 'ol/proj';
import VectorSource from 'ol/source/Vector';
import VectorLayer from 'ol/layer/Vector';
import { Feature } from 'ol';
import Point from 'ol/geom/Point';
import Style from 'ol/style/Style';
import Icon from 'ol/style/Icon'
@Component({
selector: 'app-indicator-acceuil',
templateUrl: './indicator-acceuil.component.html',
styleUrls: ['./indicator-acceuil.component.scss']
})
export class IndicatorAcceuilComponent implements OnInit {
map: any;
markerSource: VectorSource = new VectorSource({});
constructor() { }
ngOnInit(): void {
this.initilizeMap();
}
initilizeMap() {
this.map = new Map({
target: 'map',
layers: [
new Tile({
source: new OSM()
}),
new VectorLayer({
source: this.markerSource,
style: new Style({
image: new Icon({
anchor : [0.5,1],
src: 'assets/pin24x24.png',
scale : 1.5,
imgSize: [24,24],
})
})
})
],
view: new View({
center: fromLonLat([2.213749,46.227638]),
zoom: 6
}),
});
}
getCoord(event: any) {
const coordinate = this.map.getEventCoordinate(event);
let lonlat = transform(coordinate, 'EPSG:3857', 'EPSG:4326');
const lon = lonlat[0];
const lat = lonlat[1];
console.log(lonlat)
this.addMarker(lon, lat);
}
addMarker(lon: number, lat: number) {
this.markerSource.clear();
let marker = new Feature({
geometry: new Point(fromLonLat([lon,lat])) // dont worry about coordinate type 0,0 will be in west coast of africa
});
this.markerSource.addFeature(marker)
}
}
我的html文件
<div id="map" (click)="getCoord($event)"></div>
如果您像中一样在地图对象上使用OpenLayers事件侦听器,而不是使用div,那么它应该可以工作。很好,我很难实现,但它工作得很好,非常感谢