Angular& OpenLayers - forEachFeatureAtPixel和onclick属性

时间:2018-05-23 06:46:22

标签: javascript angular undefined openlayers

我正在使用Angular(6)中的OpenLayers(4.6.5)编写地图。我有一个mat-button包含一个包含两个复选框的map菜单。我想通过选中复选框来显示或隐藏某些特定标记。每个复选框都显示出某种类型的标记。当我点击一个标记时,我想要显示一个带有信息的弹出窗口(在GeoJSON文件中,每种类型的标记都有一个)。

我在一个简单的HTML文件中有一个完整的功能代码,现在我想用Angular做同样的事情,复选框是funtionnal但是当我点击一个标记时我有一个forEachFeatureAtPixel错误(如果我点击地图上的任何地方我有一个onclick错误(来自closer.onclick函数))

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 $ from 'jquery';

@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.css']
})

export class AppComponent implements OnInit {

  map: OlMap;
  gny_bright: OlWMS;
  gny_bright_mobile: OlWMS;
  wms_gnybrightl93: OlWMS;
  wms_gnybrightl93mobile: OlWMS;
  wms_gnybrightgrey: OlWMS;
  wms_gnybrightgreyl93: OlWMS;
  wms_gnyorthol93: OlWMS;
  wms_gnyorthol93_mobile: OlWMS;
  wms_pistes_c: OlWMS;
  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 = () => {
      this.popup.setPosition(undefined);
      closer.blur();
      return false;
    };

    console.log(this.popup);

    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' : '';
    });
  }
}

我在'this.popup'上放了一个console.log,以便知道问题所在,因为我收到了'undefined'... 我创建了一个弹出的当前对象,以便在我的handleSelected函数中访问弹出窗口,但这不再起作用了......

你有什么想法吗?

0 个答案:

没有答案