JSFiddle - React, Tailwind, and code Playground

by dpa99c

HTML

<span>Current position: </span>
<select id="current-pos">
    <option selected="selected">50.62062494189333, -4.7244590520858765</option>
    <option>50.62120012436721, -4.723944067955017</option>
</select>
<br/>
<hr/>
<span>Previous position: </span>
<select id="prev-pos">
    <option selected="selected" value="">None</option>
    <option>50.620286295667356, -4.724767506122589</option>
</select>
<br/>
<hr/>
<span>Target position: </span>
<input id="target-pos" value="50.6205160308103, -4.724569022655487" readonly="readonly">
<br/>
<hr/>
<span>Visit tolerance (in metres): </span>
<select id="tolerance">
    <option>10</option>
    <option>20</option>
    <option selected="selected">50</option>
    <option>100</option>
</select>
<br/>
<hr/>
<div><button id="calculate">Calculate</button></div>
<span>Result: Target location visited? </span>
<input id="result" readonly="readonly">
<br/>

CSS

select, #target-pos{
    width: 300px;
}

JavaScript

// on load
$(document).ready(function(){
    $('#calculate').click(calculate);
    calculate();
});

function calculate(){
    var currentPos = parsePosStrToLatLon($('#current-pos').val());
    var prevPos = parsePosStrToLatLon($('#prev-pos').val());
    var targetPos = parsePosStrToLatLon($('#target-pos').val());
    var tolerance = $('#tolerance').val();
    
    var visited;
    
    // Calculate if target location visited
    if(
        currentPos.distanceTo(targetPos)*1000 < tolerance ||
        (prevPos && Math.abs(targetPos.constrainedCrossTrackDistance(prevPos, currentPos)*1000) < tolerance)
    ){
        visited = true;
    }else{
        visited = false;
    }
    $('#result').val(visited.toString());
}

function parsePosStrToLatLon(posStr){    
    var result = null;
    if(posStr){
        var bits = posStr.split(", ");        
        result = new LatLon(bits[0], bits[1]);
    }
    return result;
}

/*****************************************************************************/

/*
 * Extend Number object with methods for converting degrees/radians
 */

/** Converts numeric degrees to radians */
if (typeof Number.prototype.toRad == 'undefined') {
  Number.prototype.toRad = function() {
    return this * Math.PI / 180;
  }
}

/** Converts radians to numeric (signed) degrees */
if (typeof Number.prototype.toDeg == 'undefined') {
  Number.prototype.toDeg = function() {
    return this * 180 / Math.PI;
  }
}

/** 
 * Formats the significant digits of a number, using only fixed-point notation (no exponential)
 * 
 * @param   {Number} precision: Number of significant digits to appear in the returned string
 * @returns {String} A string representation of number which contains precision significant digits
 */
if (typeof Number.prototype.toPrecisionFixed == 'undefined') {
  Number.prototype.toPrecisionFixed = function(precision) {
    
    // use standard toPrecision method
    var n = this.toPrecision(precision);
    
    // ... but replace +ve...