简体   繁体   English

Openlayers 3:无法在Angular 5应用中选择功能

[英]Openlayers 3 : cannot select a feature in an Angular 5 app

I would like to select a feature in Openlayers 3 in my Angular 5 app. 我想在Angular 5应用中的Openlayers 3中选择一个功能。 Upon clicking on a feature, I get this weird message: 单击某个功能后,我得到以下奇怪消息:

core.js:1449 ERROR TypeError: Cannot read property 'call' of undefined core.js:1449错误TypeError:无法读取未定义的属性“调用”

I have to use Openlayers3 (and not a newer version). 我必须使用Openlayers3(而不是较新的版本)。

My Html code: 我的HTML代码:

<div #mapElement id="map" class="map"></div>

The component code is: 组件代码为:

import {AfterViewInit, Component, ElementRef, ViewChild, OnInit} from '@angular/core';
declare var ol: any;

@Component({
  selector: 'app-taba-features',
  templateUrl: './taba-features.component.html',
  styleUrls: ['./taba-features.component.css']
})
export class TabaFeaturesComponent implements AfterViewInit {
  @ViewChild('mapElement') mapElement: ElementRef;
  public map: any;

  constructor() {
    // building a feature 'thing'
    const vectorSource = new ol.source.Vector({});
    const thing = new ol.geom.Polygon([[
      ol.proj.transform([6.12, 52.23], 'EPSG:4326', 'EPSG:3857'),
      ol.proj.transform([6.125, 52.24], 'EPSG:4326', 'EPSG:3857'),
      ol.proj.transform([6.13, 52.23], 'EPSG:4326', 'EPSG:3857')
    ]]);
    const featurething = new ol.Feature({
      name: 'Thing',
      geometry: thing
    });
    vectorSource.addFeature(featurething);
    // building the map
    const twello = ol.proj.transform([6.11, 52.23], 'EPSG:4326', 'EPSG:3857');
    const osmlayer = new ol.layer.Tile({
      source: new ol.source.OSM()
    });
    const view = new ol.View({
      center: twello,
      zoom: 15
    });
    this.map = new ol.Map({
      layers: [osmlayer,
        new ol.layer.Vector({
          source: vectorSource
        })],
      view: view
    });

    const that = this;
    this.map.on('click', function (evt) {
      const pixel = [evt.pixel];
      that.map.forEachFeatureAtPixel(pixel[0], pixel[1], function (feature, layer) {
        console.log('Hit'); // *** show feature name? 
      });
    });
  }

  ngAfterViewInit() {
    this.map.setTarget(this.mapElement.nativeElement.id);
  }
}

The first solution is: 一个解决方案是:

const that = this;
this.map.on('click', function(evt) {
   const pixel = evt.pixel;
   let selectedFeatures = 'Features: ';
   that.map.forEachFeatureAtPixel(pixel, function(feature, layer) {
      selectedFeatures += ' Region: ' + feature.get('N3NM');
      });
   document.getElementById('status').innerHTML = selectedFeatures;
 });

A second solution is: 第二种解决方案是:

const selectSingleClick = new ol.interaction.Select();
selectSingleClick.on('select', function (e) {
  let features = [];
  let selectedFeatures = 'Features: ';
  features = e.target.getFeatures().getArray();
  for (let i = 0; i < features.length; i++) {
     selectedFeatures += ' Region: ' + features[i].get('N3NM');
  }
  document.getElementById('status').innerHTML = selectedFeatures;
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM