0

in the below code, i want to have the variable

mLeftClicksCount

initialized to zero. i tried the following but i get value equal to NaN as shown in the image below

mLeftClicksCount : number;
mLeftClicksCount : number = 0;

please let me know how to initialze a variable of type number

code:

 this.map.on('click', function(evt){
++this.mLeftClicksCount;
if (this.mLeftClicksCount > 2) {
  this.mLeftClicksCount = 1;
  //(document.getElementById("originLngTextId") as HTMLInputElement).value = "";
  //(document.getElementById("originLngTextId") as HTMLInputElement).value = "";
}
var lonlat = transform(evt.coordinate, 'EPSG:3857', 'EPSG:4326')
this.longitude = lonlat[0];
this.latitude = lonlat[1];
console.log("evt: " , evt);
console.log("evt: " , evt.target.longitude);
console.log("evt: " , evt['target']['latitude']);
console.log("this.mLeftClicksCount: " , this.mLeftClicksCount);

if (this.mLeftClicksCount == 1) {
  (document.getElementById("originLngTextId") as HTMLInputElement).value = evt.target.longitude;
  (document.getElementById("originLatTextId") as HTMLInputElement).value = evt.target.latitude;
} else if (this.mLeftClicksCount == 2) {
  (document.getElementById("destinationLngTextId") as HTMLInputElement).value = evt.target.longitude;
  (document.getElementById("destinationLatTextId") as HTMLInputElement).value = evt.target.latitude;
}

});

image: enter image description here

Amrmsmb
  • 1
  • 27
  • 104
  • 226
  • Initialize the variable using `mLeftClicksCount : number = 0;` and use arrow function for callbacks to refer to class member variables using `this`: `this.map.on('click', (evt) => { ... })`. – ruth Mar 29 '21 at 09:11
  • @MichaelD can you please provide an example – Amrmsmb Mar 29 '21 at 09:13
  • As mentioned before you could define this as a class member variable: `mLeftClicksCount : number = 0;` and use arrow function for callback: `this.map.on('click', (evt) => { ... })`. – ruth Mar 29 '21 at 09:25
  • @MichaelD i did as you suggested but now evt is undefined ..when i log console.log("evt: " , evt.target.longitude); console.log("evt: " , evt['target']['latitude']); they are undefined..but mLeftClicksCounts can be incremented as expected – Amrmsmb Mar 29 '21 at 09:31
  • That is a different question. You need to provide more info. Like where and how is `this.map.on` triggered. – ruth Mar 29 '21 at 09:41

0 Answers0