9 Replies Latest reply on Dec 19, 2017 11:31 AM by try67

# How to find distance between two coordinates using JavaScript

I am creating a form in Adobe Acrobat. There will be four text fields; the first two will contain the latitude and longitude of a certain point, and the next two text fields will contain the latitude and longitude of a second point. I would like a fifth text field to be populated with the distance in miles between the two coordinates. How would one create script to accomplish this? I have VERY limited knowledge of JavaScript. Thanks in advance!

• ###### 1. Re: How to find distance between two coordinates using JavaScript

What is the format of these coordinates?

• ###### 2. Re: How to find distance between two coordinates using JavaScript

Decimal Degrees

• ###### 3. Re: How to find distance between two coordinates using JavaScript

... 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

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 = lon1-lon2

var radtheta = Math.PI * theta/180

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

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 = lon1-lon2
var radtheta = Math.PI * theta/180
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

Thank you much!

• ###### 7. Re: How to find distance between two coordinates using JavaScript

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

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

You can use something like this:

var dist = distance(lat1, lon1, lat2, lon2);

if (dist==0) event.value = "";

else event.value = dist;