Skip to main content
Participating Frequently
December 18, 2018
Answered

Help keeping a calculated text field blank until all conditions are met

  • December 18, 2018
  • 1 reply
  • 512 views

I have a form in which the latitude and longitude for a starting point can be entered, followed by the latitude and longitude for an ending point. The distance between the two coordinates is then automatically calculated. As it is right now, If I enter a value in the first latitude column and the first longitude column, the distance column will populate with a value, even though the second latitude and longitude columns are blank. I would like the distance column to remain blank until a complete data set has been entered. If anyone could help me with this, I'd surely appreciate it.  

Script for the distance text field of the first row:

function distance(lat1, lon1, lat2, lon2, unit) {
    var radlat1 = Math.PI * lat1/180
    var radlat2 = Math.PI * lat2/180
    var theta = lon1-lon2
    var radtheta = Math.PI * theta/180
    var dist = Math.sin(radlat1) * Math.sin(radlat2) + Math.cos(radlat1) * Math.cos(radlat2) * Math.cos(radtheta);
    dist = Math.acos(dist)
    dist = dist * 180/Math.PI
    dist = dist * 60 * 1.1515
    if (unit=="K") { dist = dist * 1.609344 }
    if (unit=="N") { dist = dist * 0.8684 }
    return dist
}

var lat1 = this.getField("Alat1.0").value
var lon1 = this.getField("Alon1.0").value
var lat2 = this.getField("Alat2.0").value
var lon2 = this.getField("Alon2.0").value
//Assuming Miles
this.getField("rel1.0").value = distance(lat1, lon1, lat2, lon2)

This topic has been closed for replies.
Correct answer try67

Use this code:

var lat1 = this.getField("Alat1.0").valueAsString;

var lon1 = this.getField("Alon1.0").valueAsString;

var lat2 = this.getField("Alat2.0").valueAsString;

var lon2 = this.getField("Alon2.0").valueAsString;

//Assuming Miles

if (lat1!="" && lon1!="" && lat2!="" && lon2!="")

    this.getField("rel1.0").value = distance(+lat1, +lon1, +lat2, +lon2);

else this.getField("rel1.0").value = "";

1 reply

try67
Community Expert
try67Community ExpertCorrect answer
Community Expert
December 18, 2018

Use this code:

var lat1 = this.getField("Alat1.0").valueAsString;

var lon1 = this.getField("Alon1.0").valueAsString;

var lat2 = this.getField("Alat2.0").valueAsString;

var lon2 = this.getField("Alon2.0").valueAsString;

//Assuming Miles

if (lat1!="" && lon1!="" && lat2!="" && lon2!="")

    this.getField("rel1.0").value = distance(+lat1, +lon1, +lat2, +lon2);

else this.getField("rel1.0").value = "";

samW245Author
Participating Frequently
December 18, 2018

Complete novice here...I struggle with it for hours, you do it in 5 minutes! Thanks!