
1. Re: How to find distance between two coordinates using JavaScript
try67 Dec 18, 2017 9:07 AM (in response to samW245)What is the format of these coordinates?

2. Re: How to find distance between two coordinates using JavaScript
samW245 Dec 18, 2017 9:12 AM (in response to try67)Decimal Degrees

3. Re: How to find distance between two coordinates using JavaScript
Joel_Geraci Dec 18, 2017 9:13 AM (in response to samW245)Google turned up this... http://www.geodatasource.com/developers/javascript
Calculate Distance by Latitude and Longitude using JavaScript  GeoDataSource
... to do the calculation but first you'll need get the variables. Replace the field names below with your own.
var lat1 = this.getField("First Lat Field Name").value;
var lon1 = this.getField("First Lon Field Name").value;
do the same for lon2 and lat 2 then set the fifth field like this.
this.getField("Fifth Field").value = distance(lat1, lon1, lat2, lon2, unit);

4. Re: How to find distance between two coordinates using JavaScript
samW245 Dec 18, 2017 9:53 AM (in response to Joel_Geraci)Here are my field names, in order:
 Alat1
 Alon1
 Alat2
 Alon2
 rel1
So if I'm understanding correctly, this is what should be entered as custom calculation script for the fifth text field?
function distance(lat1, lon1, lat2, lon2, unit) {
var lat1 = this.getField(Alat1).value;
var lon1 = this.getField(Alon1).value;
var lat2 = this.getField(Alat2).value;
var lon2 = this.getField(Alon2).value;
this.getField(rel1).value = distance(lat1, lon1, lat2, lon2, unit);
var radlat1 = Math.PI * lat1/180
var radlat2 = Math.PI * lat2/180
var theta = lon1lon2
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
}

5. Re: How to find distance between two coordinates using JavaScript
Joel_Geraci Dec 18, 2017 11:20 AM (in response to samW245)No. Spend an hour with a JavaScript tutorial before trying to adapt any code you find online. You'll be less frustrated. There's a god one here...
Then here's the script you'll need.
function distance(lat1, lon1, lat2, lon2, unit) { var radlat1 = Math.PI * lat1/180 var radlat2 = Math.PI * lat2/180 var theta = lon1lon2 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").value var lon1 = this.getField("Alon1").value var lat2 = this.getField("Alat2").value var lon2 = this.getField("Alon2").value //Assuming Miles this.getField("rel1").value = distance(lat1, lon1, lat2, lon2)

6. Re: How to find distance between two coordinates using JavaScript
samW245 Dec 18, 2017 2:45 PM (in response to Joel_Geraci)Thank you much!

7. Re: How to find distance between two coordinates using JavaScript
samW245 Dec 19, 2017 8:32 AM (in response to samW245)One additional question: when the text fields where the coordinates are entered are blank, the distance text field displays a 0; does anyone know of a way to have the distance text field be blank when there is no data in the coordinate text fields?

8. Re: How to find distance between two coordinates using JavaScript
samW245 Dec 19, 2017 11:04 AM (in response to samW245)I've tried this, but I'm getting inconsistent results:
if (getField("Alat1.0").valueAsString === "")
if (getField("Alon1.0").valueAsString === "")
if (getField("Alat2.0").valueAsString === "")
if (getField("Alon2.0").valueAsString === "")
{
event.value = "";
} else {
event.value = dist;
}

9. Re: How to find distance between two coordinates using JavaScript
try67 Dec 19, 2017 11:31 AM (in response to samW245)You can use something like this:
var dist = distance(lat1, lon1, lat2, lon2);
if (dist==0) event.value = "";
else event.value = dist;