Skip to main content
Participating Frequently
Dec 18, 2017 5:02 PM UTC
Question

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!

This topic is closed to new replies. Start a new post to keep the conversation going.

9 replies

samW245Author
Participating Frequently
Dec 19, 2017 4:32 PM UTC

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?

Translate
samW245Author
Participating Frequently
Dec 19, 2017 7:04 PM UTC

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;

}

Translate
try67
Community Expert
Community Expert
Dec 19, 2017 7:31 PM UTC

You can use something like this:

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

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

else event.value = dist;

Translate
Joel Geraci
Community Expert
Community Expert
Dec 18, 2017 5:13 PM UTC

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);

Translate
samW245Author
Participating Frequently
Dec 18, 2017 5:53 PM UTC

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

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

}

Translate
Joel Geraci
Community Expert
Community Expert
Dec 18, 2017 7:20 PM UTC

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

JavaScript Tutorial

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

    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)

Translate
try67
Community Expert
Community Expert
Dec 18, 2017 5:07 PM UTC

What is the format of these coordinates?

Translate
samW245Author
Participating Frequently
Dec 18, 2017 5:12 PM UTC

Decimal Degrees

Translate