this.sceneView.hitTest(screenPoint): Uncaught TypeError

656
2
Jump to solution
02-09-2017 03:11 PM
by Anonymous User
Not applicable

I created a small application using JavaScript API 4.2 (3D) which works fine.  Now, I am trying to replicate the same within WAB.  However, I have a problem executing sceneView.hitTest() function.  it gave me an "Uncaught TypeError".  I am new to WAB so I can't figure out what I am doing wrong.  I would greatly appreciate any inputs.  

Here is the error I am getting and code. 

onOpen: function(){ 
   console.log('onOpen');
   this.sceneView.on("click", function(event){ 
        var screenPoint = {x: event.x, y: event.y};         
        console.log(screenPoint);         
        this.sceneView.hitTest(screenPoint).then(function(res){
           console.log(res.results[0]);
         });
    });      
},

Thank you,
Makiko
0 Kudos
1 Solution

Accepted Solutions
RobertScheitlin__GISP
MVP Emeritus

Makiko,

  Your issue is the scope of "this" has changed inside your sceneView click event handler. So to get past that you need to use lang.hitch:

onOpen: function(){ 
   console.log('onOpen');
   this.sceneView.on("click", lang.hitch(this, function(event){ 
        var screenPoint = {x: event.x, y: event.y};         
        console.log(screenPoint);         
        this.sceneView.hitTest(screenPoint).then(function(res){
           console.log(res.results[0]);
         });
    }));      
},

View solution in original post

0 Kudos
2 Replies
RobertScheitlin__GISP
MVP Emeritus

Makiko,

  Your issue is the scope of "this" has changed inside your sceneView click event handler. So to get past that you need to use lang.hitch:

onOpen: function(){ 
   console.log('onOpen');
   this.sceneView.on("click", lang.hitch(this, function(event){ 
        var screenPoint = {x: event.x, y: event.y};         
        console.log(screenPoint);         
        this.sceneView.hitTest(screenPoint).then(function(res){
           console.log(res.results[0]);
         });
    }));      
},
0 Kudos
by Anonymous User
Not applicable

Robert,

Learning lang.hitch is game changing!! Thank you so much!!  

0 Kudos