我正在使用包含两个复选框的菜单编程地图以显示或隐藏标记(当我点击标记时,我会显示GeoJSON文件中包含的一些信息)。我在HTML文件中有一个简单的版本,工作正常,现在我想在Angular中做同样的事情。
我已经调整了我的代码,除弹出窗口外,所有代码都正常工作,forEachFeatureAtPixel函数存在问题。
app.component.ts:
import { Component, OnInit } from '@angular/core';
import OlMap from 'ol/map';
import OlWMS from 'ol/source/tilewms';
import OlTileLayer from 'ol/layer/tile';
import OlView from 'ol/view';
import VectorLayer from 'ol/layer/vector';
import VectorSource from 'ol/source/vector';
import Point from 'ol/geom/point';
import Style from 'ol/style/style';
import IconStyle from 'ol/style/icon';
import WFS from 'ol/format/wfs';
import GeoJSON from 'ol/format/geojson';
import Overlay from 'ol/overlay';
import feature from 'ol/feature';
import OlSwitch from 'ol-layerswitcher';
import Group from 'ol/layer/group';
import proj from 'ol/proj';
import $ from 'jquery';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
map: OlMap;
layer: OlTileLayer;
view: OlView;
layerSwitcher: OlSwitch;
WFS: WFS;
vectorLayer_parking: VectorLayer;
vectorLayer_piscine: VectorLayer;
parkingLayer: VectorSource;
piscineLayer: VectorSource;
piscine: Style;
markers: feature;
popup: Overlay;
constructor() {
}
handleSelected1($event) {
if($event.target.checked === true) {
this.map.addControl(this.vectorLayer_piscine);
this.vectorLayer_piscine.setStyle(this.piscine);
this.map.addOverlay(this.popup);
} else {
this.map.removeControl(this.vectorLayer_piscine);
this.map.removeOverlay(this.popup);
}
}
handleSelected2($event) {
if($event.target.checked === true) {
this.map.addControl(this.vectorLayer_parking);
this.vectorLayer_parking.setStyle(this.markers);
this.map.addOverlay(this.popup);
} else {
this.map.removeControl(this.vectorLayer_parking);
this.map.removeOverlay(this.popup);
}
}
ngOnInit() {
{...}
//popup
var element = document.getElementById('popup');
this.popup = new Overlay({
element: element,
autoPan: true,
offset: [0, -30]
});
//Fonction d'affichage des popups
var content_element = document.getElementById('popup-content');
this.map.on('click', function(evt){
var closer = document.getElementById('popup-closer');
closer.onclick = function() {
this.popup.setPosition(undefined); //where the problem is
closer.blur();
return false;
};
var feature = this.map.forEachFeatureAtPixel(evt.pixel,
function(feature) {
return feature;
});
if (feature) {
var geometry = feature.getGeometry();
var coord = geometry.getCoordinates();
if(feature.get('name') != null) {
var content = '<center><h2>' + feature.get('name') + '</h2></center>' + '<br>';
} else {
var content = '<center><h2>' + feature.get('NOM') + '</h2></center>' + '<br>';
}
if(feature.get('addr:street') != null) {
content += '<h5>' + '<i>Adresse : </i>' + feature.get('addr:street') + '</h5>';
} else if(feature.get('ADRESSE') != null) {
content += '<h5>' + '<i>Adresse : </i>' + feature.get('ADRESSE') + '</h5>';
} else {
null;
}
if(feature.get('phone') != null) {
content += '<h5>' + '<i>Numéro de téléphone : </i>' + feature.get('phone') + '</h5>';
}
if(feature.get('website') != null) {
content += '<h5>' + '<i>Site internet : </i>' + '</h5>' + '<p>' + feature.get('website') + '</p>';
}
if(feature.get('CAPACITE')!=null) {
content += '<h5>' + '<i>Capacité : </i>' + feature.get('CAPACITE') + '</h5>';
}
if(feature.get('PLACES')!=null) {
content += '<h5>' + '<i>Places disponibles : </i>' + feature.get('PLACES') + '<h5>';
}
content_element = document.getElementById('popup-content');
content_element.innerHTML = content;
this.popup.setPosition(coord);
}
});
this.map.on('pointermove', (e) => {
if (e.dragging) {
return;
};
var pixel = this.map.getEventPixel(e.originalEvent);
var hit = this.map.hasFeatureAtPixel(pixel);
this.map.getViewport().style.cursor = hit ? 'pointer' : '';
});
}
}
问题在于close.onclick函数,其中我遇到this.popup
的问题([ts]“HTMLElement”类型中不存在属性'popup')...
你有什么想法吗?