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









up vote
0
down vote

favorite












I would like to select a feature in Openlayers 3 in my Angular 5 app. Upon clicking on a feature, I get this weird message:




core.js:1449 ERROR TypeError: Cannot read property 'call' of undefined




I have to use Openlayers3 (and not a newer version).



My Html code:



<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);











share|improve this question



























    up vote
    0
    down vote

    favorite












    I would like to select a feature in Openlayers 3 in my Angular 5 app. Upon clicking on a feature, I get this weird message:




    core.js:1449 ERROR TypeError: Cannot read property 'call' of undefined




    I have to use Openlayers3 (and not a newer version).



    My Html code:



    <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);











    share|improve this question

























      up vote
      0
      down vote

      favorite









      up vote
      0
      down vote

      favorite











      I would like to select a feature in Openlayers 3 in my Angular 5 app. Upon clicking on a feature, I get this weird message:




      core.js:1449 ERROR TypeError: Cannot read property 'call' of undefined




      I have to use Openlayers3 (and not a newer version).



      My Html code:



      <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);











      share|improve this question















      I would like to select a feature in Openlayers 3 in my Angular 5 app. Upon clicking on a feature, I get this weird message:




      core.js:1449 ERROR TypeError: Cannot read property 'call' of undefined




      I have to use Openlayers3 (and not a newer version).



      My Html code:



      <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);








      angular openlayers-3






      share|improve this question















      share|improve this question













      share|improve this question




      share|improve this question








      edited Nov 9 at 14:55

























      asked Nov 9 at 13:50









      tjm1706

      2,59932156




      2,59932156






















          1 Answer
          1






          active

          oldest

          votes

















          up vote
          0
          down vote



          accepted










          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;
          );





          share|improve this answer




















            Your Answer






            StackExchange.ifUsing("editor", function ()
            StackExchange.using("externalEditor", function ()
            StackExchange.using("snippets", function ()
            StackExchange.snippets.init();
            );
            );
            , "code-snippets");

            StackExchange.ready(function()
            var channelOptions =
            tags: "".split(" "),
            id: "1"
            ;
            initTagRenderer("".split(" "), "".split(" "), channelOptions);

            StackExchange.using("externalEditor", function()
            // Have to fire editor after snippets, if snippets enabled
            if (StackExchange.settings.snippets.snippetsEnabled)
            StackExchange.using("snippets", function()
            createEditor();
            );

            else
            createEditor();

            );

            function createEditor()
            StackExchange.prepareEditor(
            heartbeatType: 'answer',
            convertImagesToLinks: true,
            noModals: true,
            showLowRepImageUploadWarning: true,
            reputationToPostImages: 10,
            bindNavPrevention: true,
            postfix: "",
            imageUploader:
            brandingHtml: "Powered by u003ca class="icon-imgur-white" href="https://imgur.com/"u003eu003c/au003e",
            contentPolicyHtml: "User contributions licensed under u003ca href="https://creativecommons.org/licenses/by-sa/3.0/"u003ecc by-sa 3.0 with attribution requiredu003c/au003e u003ca href="https://stackoverflow.com/legal/content-policy"u003e(content policy)u003c/au003e",
            allowUrls: true
            ,
            onDemand: true,
            discardSelector: ".discard-answer"
            ,immediatelyShowMarkdownHelp:true
            );



            );













             

            draft saved


            draft discarded


















            StackExchange.ready(
            function ()
            StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226992%2fopenlayers-3-cannot-select-a-feature-in-an-angular-5-app%23new-answer', 'question_page');

            );

            Post as a guest






























            1 Answer
            1






            active

            oldest

            votes








            1 Answer
            1






            active

            oldest

            votes









            active

            oldest

            votes






            active

            oldest

            votes








            up vote
            0
            down vote



            accepted










            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;
            );





            share|improve this answer
























              up vote
              0
              down vote



              accepted










              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;
              );





              share|improve this answer






















                up vote
                0
                down vote



                accepted







                up vote
                0
                down vote



                accepted






                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;
                );





                share|improve this answer












                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;
                );






                share|improve this answer












                share|improve this answer



                share|improve this answer










                answered Nov 10 at 18:47









                tjm1706

                2,59932156




                2,59932156



























                     

                    draft saved


                    draft discarded















































                     


                    draft saved


                    draft discarded














                    StackExchange.ready(
                    function ()
                    StackExchange.openid.initPostLogin('.new-post-login', 'https%3a%2f%2fstackoverflow.com%2fquestions%2f53226992%2fopenlayers-3-cannot-select-a-feature-in-an-angular-5-app%23new-answer', 'question_page');

                    );

                    Post as a guest














































































                    Popular posts from this blog

                    Use pre created SQLite database for Android project in kotlin

                    Darth Vader #20

                    Ondo