Select to view content in your preferred language

Set interval for tracking widget position logging?

716
3
Jump to solution
06-12-2017 07:12 AM
FranklinAlexander
Occasional Contributor III

Looking for some advice here, as I am still learning Javascript, but have been tasked with making some changes to a widget that was authored by one of my predecessors. The widget tracks your path, logs the positions in a log file, and creates a csv file for export. The watchPosition() method takes positions constantly, which while great for accuracy, creates way too many entries in the log file and the csv file. I am looking for the best way to limit logged entries by either setting some sort of logging interval based on time, or by counting loops. I have been playing around with different scripts, and have found one that works fine in a testing environment, but when I insert it into the widget.js file it doesn't seem to do anything. The script basically takes a time stamp before the loop, and another one each time the loop repeats. I set up an array to capture the 'distance' between each time stamp and to capture only unique values (seconds). I then have it set up to log the position whenever the last added value entered into the array is divisible by 2.

The problem I am having is that the variables I defined before the loop aren't being recognized inside the loop, hence the distArray never gets populated. I don't know if this is a hoisting issue, or maybe has to do with the asynchronous nature of the widget.js code. Here is a snippet of my code, and I have also attached the widget.js file. Not looking for anyone to write me code for me, but I am probably must missing something that may be obvious to you guys! Thanks 

targetlayer.queryFeatures(query, lang.hitch(this, function(featureSet) {   

      var ResultFeatures = featureSet.features;
      var currenttime = this.TimeStamp();
      var distArray = [];
      var startTime = new Date().getSeconds();

      for(var i = 0; i < ResultFeatures.length; i++){
            var loopTime = new Date().getSeconds();
            var distance = loopTime - startTime;
            var currentLoc = (currenttime + ',' + PositionSet[0].toPrecision(8) + ',' + PositionSet[1].toPrecision(8) + ',' +                   targetlayer.name + ',' + ResultFeatures.attributes[resultField]);
            var currentLocArray = currentLoc.split(",");
            resultsArray.push(currentLocArray);
            var logValue = this.Log.value += '\r\n' + currentLoc;

            if (distArray.indexOf(distance) === -1) {
                   distArray.push(distance);

                   if (distance % 2 == 0) {
                         logValue;

                   } else {
                         continue;
                   }
             }

0 Kudos
1 Solution

Accepted Solutions
JordanBaumgardner
Occasional Contributor III

I took a quick look at your code and didn't see anything obvious with regards to variable scoping. But, you may want to take a different approach. If you limited your log entries by Distance and not Time you would save a lot of space. Meaning, Only log points that are X meters apart from the last logged point.

View solution in original post

0 Kudos
3 Replies
JordanBaumgardner
Occasional Contributor III

I took a quick look at your code and didn't see anything obvious with regards to variable scoping. But, you may want to take a different approach. If you limited your log entries by Distance and not Time you would save a lot of space. Meaning, Only log points that are X meters apart from the last logged point.

0 Kudos
FranklinAlexander
Occasional Contributor III

Thanks Jordan. I like your idea about using the distance between points, it make more sense in terms of consistency since logging based on time doesn't take speed of the person being tracked into account. It was actually more code because of the distance calculation, but it works and that's all that matters! 

0 Kudos
RebeccaStrauch__GISP
MVP Emeritus

Although probably not as much of an issue as in the past, and a totally different software (ArcPad), but I also had to test for extremely long distances between collected points. This was because I measured the distance from the start of a "transect" and let the user know when they had reached a certain distance, that is, summed the distance.  Occasionally we get a bogus GPS x/y coord that seems to be out in the ozone somewhere.  This would crash the software since the distance between the points was 500 billion meters or some such number of digits.  (Also was collecting and testing speed).  I would always check for values that were out of possible range and throw out (not record) that "bad" location. Just something to keep in mind....and sorry, not offering any suggest code.  Mine code is vba/vbs and js for the ArcPad app.