Javascript 角度-在复选框中添加事件
我正在用一个带有OpenLayers的Angular地图应用程序编程,我想在复选框中添加一些事件。我创建了一个mat按钮,里面有一个mat菜单,包含两个复选框 所有地图组件都位于app.component.ts文件中,带有复选框的“我的菜单”将创建为app.component.html文件 app.component.html:Javascript 角度-在复选框中添加事件,javascript,jquery,angular,openlayers,Javascript,Jquery,Angular,Openlayers,我正在用一个带有OpenLayers的Angular地图应用程序编程,我想在复选框中添加一些事件。我创建了一个mat按钮,里面有一个mat菜单,包含两个复选框 所有地图组件都位于app.component.ts文件中,带有复选框的“我的菜单”将创建为app.component.html文件 app.component.html: <!DOCTYPE html> <html> <head> <meta charset="utf-8">
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<div class="header">
<mat-toolbar>OpenLayers</mat-toolbar>
<div class="menu">
<button mat-button [matMenuTriggerFor]="menu">Marqueurs</button>
<mat-menu #menu="matMenu">
<input type="checkbox" id="piscine" name="piscine" value="piscine">
<label for="subscribeNews">Piscines</label>
<br>
<input type="checkbox" id="piscine" name="piscine" value="piscine">
<label for="subscribeNews">Parkings</label>
</mat-menu>
</div>
</div>
<div id="map" class="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
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 olProj from 'ol/proj';
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;
layer1: OlWMS;
layer2: OlWMS;
layer3: OlWMS;
layer: OlTileLayer;
view: OlView;
layerSwitcher: OlSwitch;
WFS: WFS;
vectorLayer_parking: VectorLayer;
vectorLayer_piscine: VectorLayer;
parkingLayer: VectorSource;
piscineLayer: VectorSource;
constructor() {
}
ngOnInit() {
this.layer1 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
this.layer2 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
this.layer3 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
//view
this.view = new OlView({
center: [0, 0],
minZoom: 11,
maxZoom: 19,
zoom: 12
});
this.parkingLayer = new VectorSource({
url: '...',
format: new GeoJSON()
});
this.piscineLayer = new VectorSource({
url: '...',
format: new GeoJSON()
});
this.vectorLayer_parking = new VectorLayer({
source: this.parkingLayer
});
this.vectorLayer_piscine = new VectorLayer({
source: this.piscineLayer
});
this.map = new OlMap({
target: 'map',
layers: [new Group({
title: 'Fonds de carte',
layers: [
new OlTileLayer({
title: 'Layer1',
source: this.layer1,
type: 'base'
}),
new OlTileLayer({
title: 'Layer2',
source: this.layer2,
type: 'base'
})
]
}),
new Group({
title: 'Surcouche',
layers: [
new OlTileLayer({
title: 'Layer3',
source: this.layer3,
format: new WFS(),
visible: false
})
]
}),
],
view: this.view
});
//popup
var element = document.getElementById('popup');
var 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() {
popup.setPosition(undefined);
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;
popup.setPosition(coord);
}
});
handleSelected($event) {
if($event.target.checked === true) {
this.map.addControl(this.vectorLayer_piscine);
this.vectorLayer_piscine.setStyle(piscine);
this.map.addOverlay(popup);
} else {
this.map.removeControl(this.vectorLayer_piscine);
this.map.removeOverlay(popup);
}
}
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' : '';
});
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<div class="header">
<mat-toolbar>OpenLayers</mat-toolbar>
<div class="menu">
<button mat-button [matMenuTriggerFor]="menu">Marqueurs</button>
<mat-menu #menu="matMenu">
<input type="checkbox" id="piscine" name="piscine" (change)="handleSelected($event)">
<label for="subscribeNews">Piscines</label>
<br>
<input type="checkbox" id="parking" name="parking">
<label for="subscribeNews">Parkings</label>
</mat-menu>
</div>
</div>
<div id="map" class="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
对于jQuery的导入:import$来自“jQuery”代码>(我使用了npm安装jquery
)
使用此代码,我希望仅在选中相应的复选框时,在地图上显示一些标记
有没有其他方法可以检索复选框状态?首先,我可以看到两种方法
<input type="checkbox" id="piscine" name="piscine" value="piscine">
希望这对你有帮助 app.component.ts:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<div class="header">
<mat-toolbar>OpenLayers</mat-toolbar>
<div class="menu">
<button mat-button [matMenuTriggerFor]="menu">Marqueurs</button>
<mat-menu #menu="matMenu">
<input type="checkbox" id="piscine" name="piscine" value="piscine">
<label for="subscribeNews">Piscines</label>
<br>
<input type="checkbox" id="piscine" name="piscine" value="piscine">
<label for="subscribeNews">Parkings</label>
</mat-menu>
</div>
</div>
<div id="map" class="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
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 olProj from 'ol/proj';
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;
layer1: OlWMS;
layer2: OlWMS;
layer3: OlWMS;
layer: OlTileLayer;
view: OlView;
layerSwitcher: OlSwitch;
WFS: WFS;
vectorLayer_parking: VectorLayer;
vectorLayer_piscine: VectorLayer;
parkingLayer: VectorSource;
piscineLayer: VectorSource;
constructor() {
}
ngOnInit() {
this.layer1 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
this.layer2 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
this.layer3 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
//view
this.view = new OlView({
center: [0, 0],
minZoom: 11,
maxZoom: 19,
zoom: 12
});
this.parkingLayer = new VectorSource({
url: '...',
format: new GeoJSON()
});
this.piscineLayer = new VectorSource({
url: '...',
format: new GeoJSON()
});
this.vectorLayer_parking = new VectorLayer({
source: this.parkingLayer
});
this.vectorLayer_piscine = new VectorLayer({
source: this.piscineLayer
});
this.map = new OlMap({
target: 'map',
layers: [new Group({
title: 'Fonds de carte',
layers: [
new OlTileLayer({
title: 'Layer1',
source: this.layer1,
type: 'base'
}),
new OlTileLayer({
title: 'Layer2',
source: this.layer2,
type: 'base'
})
]
}),
new Group({
title: 'Surcouche',
layers: [
new OlTileLayer({
title: 'Layer3',
source: this.layer3,
format: new WFS(),
visible: false
})
]
}),
],
view: this.view
});
//popup
var element = document.getElementById('popup');
var 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() {
popup.setPosition(undefined);
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;
popup.setPosition(coord);
}
});
handleSelected($event) {
if($event.target.checked === true) {
this.map.addControl(this.vectorLayer_piscine);
this.vectorLayer_piscine.setStyle(piscine);
this.map.addOverlay(popup);
} else {
this.map.removeControl(this.vectorLayer_piscine);
this.map.removeOverlay(popup);
}
}
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' : '';
});
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<div class="header">
<mat-toolbar>OpenLayers</mat-toolbar>
<div class="menu">
<button mat-button [matMenuTriggerFor]="menu">Marqueurs</button>
<mat-menu #menu="matMenu">
<input type="checkbox" id="piscine" name="piscine" (change)="handleSelected($event)">
<label for="subscribeNews">Piscines</label>
<br>
<input type="checkbox" id="parking" name="parking">
<label for="subscribeNews">Parkings</label>
</mat-menu>
</div>
</div>
<div id="map" class="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
从'@angular/core'导入{Component,OnInit};
从“ol/map”导入OlMap;
从“ol/source/tilewms”导入OlWMS;
从“ol/layer/tile”导入OlTileLayer;
从“ol/view”导入OlView;
从“ol/proj”导入olProj;
从“ol/layer/vector”导入VectorLayer;
从“ol/source/vector”导入VectorSource;
从“ol/geom/Point”导入点;
从“ol/Style/Style”导入样式;
从“ol/style/icon”导入IconStyle;
从“ol/format/WFS”导入WFS;
从“ol/format/GeoJSON”导入GeoJSON;
从“ol/Overlay”导入覆盖;
从“ol/功能”导入功能;
从“ol layerswitcher”导入OlSwitch;
从“ol/层/组”导入组;
从“ol/proj”导入项目;
从“jquery”导入美元;
@组成部分({
选择器:'应用程序根',
templateUrl:“./app.component.html”,
样式URL:['./app.component.css']
})
导出类AppComponent实现OnInit{
地图:OlMap;
第1层:OlWMS;
第二层:嗅觉;
第3层:OlWMS;
图层:OlTileLayer;
视图:OlView;
分层开关:OlSwitch;
WFS:WFS;
矢量层\停车场:矢量层;
矢量层\鱼:矢量层;
停车层:矢量源;
鱼层:矢量源;
构造函数(){
}
恩戈尼尼特(){
this.layer1=新的OlWMS({
url:“…”,
参数:{…},
属性:“…”
});
this.layer2=新的OlWMS({
url:“…”,
参数:{…},
属性:“…”
});
this.layer3=新的OlWMS({
url:“…”,
参数:{…},
属性:“…”
});
//看法
this.view=新视图({
中间:[0,0],
minZoom:11,
maxZoom:19,
缩放:12
});
this.parkingLayer=新矢量源({
url:“…”,
格式:new GeoJSON()
});
this.piscineLayer=新矢量源({
url:“…”,
格式:new GeoJSON()
});
this.vectorLayer\u parking=新矢量层({
来源:这是parkingLayer
});
this.vectorLayer\u piscine=新矢量层({
资料来源:这是一只鱼
});
this.map=new OlMap({
目标:“地图”,
层:[新组({
标题:“点菜坊”,
图层:[
新OlTileLayer({
标题:“第1层”,
资料来源:这是layer1,
类型:“基本”
}),
新OlTileLayer({
标题:“第二层”,
资料来源:这是第2层,
类型:“基本”
})
]
}),
新组({
标题:“Surcouche”,
图层:[
新OlTileLayer({
标题:“第三层”,
资料来源:这是第3层,
格式:新WFS(),
可见:假
})
]
}),
],
视图:this.view
});
//弹出窗口
var元素=document.getElementById('popup');
var popup=新覆盖({
元素:元素,
自动扫描:是的,
偏移量:[0,-30]
});
//弹出窗口附加功能
var content_element=document.getElementById('popup-content');
this.map.on('click',函数(evt){
var closer=document.getElementById('popup-closer');
closer.onclick=函数(){
popup.setPosition(未定义);
closer.blur();
返回false;
};
var feature=this.map.forEachFeatureAtPixel(evt.pixel,
功能(特征){
返回特性;
});
如果(功能){
var geometry=feature.getGeometry();
var coord=geometry.getCoordinates();
if(feature.get('name')!=null){
var content=''+feature.get('name')++'
;
}否则{
var content=''+feature.get('NOM')++'+'
';
}
if(feature.get('addr:street')!=null){
content+=''+'地址:'+feature.get('addr:street')+'';
}else if(feature.get('address')!=null){
content+=''+'地址:'+feature.get('adrese')+'';
}否则{
无效的
}
if(feature.get('phone')!=null){
content+=''+'手机号码:'+功能。get('phone')+'';
}
if(feature.get('website')!=null){
内容+=''+'网站互联网:'+'+''+功能。获取('website')+'';
}
if(feature.get('capacity')!=null){
content+=''+'电容:'+功能。get('capacity')+'';
}
if(feature.get('PLACES')!=null){
content+=''+'放置争议文件:'+feature.get('Places')+'';
}
content_element=document.getElementById('popup-content');
content\u element.innerHTML=内容;
弹出设置位置(坐标);
}
});
未选手($事件){
如果($event.target.checked==true){
this.map.addControl(this.vectorLayer\u piscine);
这是一个向量机鱼设置方式(鱼);
this.map.addOverlay(弹出窗口);
}否则{
这个.map.removeControl(这个.vectorLayer\u piscine);
this.map.removeOverlay(弹出窗口);
}
}
this.map.on('pointermove',(e)=>{
如果(如拖动){
回来
};
var pixel=this.map.getEventPixel(e.originalEvent);
var hit=this.map.hasFeatureAtPixel(像素);
this.map.getViewport().style.cursor=点击“指针”:“”;
});
}
}
app.component.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<div class="header">
<mat-toolbar>OpenLayers</mat-toolbar>
<div class="menu">
<button mat-button [matMenuTriggerFor]="menu">Marqueurs</button>
<mat-menu #menu="matMenu">
<input type="checkbox" id="piscine" name="piscine" value="piscine">
<label for="subscribeNews">Piscines</label>
<br>
<input type="checkbox" id="piscine" name="piscine" value="piscine">
<label for="subscribeNews">Parkings</label>
</mat-menu>
</div>
</div>
<div id="map" class="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
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 olProj from 'ol/proj';
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;
layer1: OlWMS;
layer2: OlWMS;
layer3: OlWMS;
layer: OlTileLayer;
view: OlView;
layerSwitcher: OlSwitch;
WFS: WFS;
vectorLayer_parking: VectorLayer;
vectorLayer_piscine: VectorLayer;
parkingLayer: VectorSource;
piscineLayer: VectorSource;
constructor() {
}
ngOnInit() {
this.layer1 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
this.layer2 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
this.layer3 = new OlWMS({
url: '...',
params: {...},
attributions: '...'
});
//view
this.view = new OlView({
center: [0, 0],
minZoom: 11,
maxZoom: 19,
zoom: 12
});
this.parkingLayer = new VectorSource({
url: '...',
format: new GeoJSON()
});
this.piscineLayer = new VectorSource({
url: '...',
format: new GeoJSON()
});
this.vectorLayer_parking = new VectorLayer({
source: this.parkingLayer
});
this.vectorLayer_piscine = new VectorLayer({
source: this.piscineLayer
});
this.map = new OlMap({
target: 'map',
layers: [new Group({
title: 'Fonds de carte',
layers: [
new OlTileLayer({
title: 'Layer1',
source: this.layer1,
type: 'base'
}),
new OlTileLayer({
title: 'Layer2',
source: this.layer2,
type: 'base'
})
]
}),
new Group({
title: 'Surcouche',
layers: [
new OlTileLayer({
title: 'Layer3',
source: this.layer3,
format: new WFS(),
visible: false
})
]
}),
],
view: this.view
});
//popup
var element = document.getElementById('popup');
var 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() {
popup.setPosition(undefined);
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;
popup.setPosition(coord);
}
});
handleSelected($event) {
if($event.target.checked === true) {
this.map.addControl(this.vectorLayer_piscine);
this.vectorLayer_piscine.setStyle(piscine);
this.map.addOverlay(popup);
} else {
this.map.removeControl(this.vectorLayer_piscine);
this.map.removeOverlay(popup);
}
}
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' : '';
});
}
}
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<meta name="viewport" content="width=device-width, initial-scale=1.0">
<link rel="stylesheet" href="https://stackpath.bootstrapcdn.com/bootstrap/4.1.0/css/bootstrap.min.css" integrity="sha384-9gVQ4dYFwwWSjIDZnLEWnxCjeSWFphJiwGPXr1jddIhOegiu1FwO5qRGvFXOdJZ4" crossorigin="anonymous">
</head>
<body>
<div class="header">
<mat-toolbar>OpenLayers</mat-toolbar>
<div class="menu">
<button mat-button [matMenuTriggerFor]="menu">Marqueurs</button>
<mat-menu #menu="matMenu">
<input type="checkbox" id="piscine" name="piscine" (change)="handleSelected($event)">
<label for="subscribeNews">Piscines</label>
<br>
<input type="checkbox" id="parking" name="parking">
<label for="subscribeNews">Parkings</label>
</mat-menu>
</div>
</div>
<div id="map" class="map"></div>
<div id="popup" class="ol-popup">
<a href="#" id="popup-closer" class="ol-popup-closer"></a>
<div id="popup-content"></div>
</div>
</body>
</html>
打开