How to find distance between two coordinates using JavaScript

New Here ,
Dec 18, 2017

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript

Views

263

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more

How to find distance between two coordinates using JavaScript

New Here ,
Dec 18, 2017

Copy link to clipboard

Copied

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!

TOPICS
Acrobat SDK and JavaScript

Views

264

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Dec 18, 2017 0
Most Valuable Participant ,
Dec 18, 2017

Copy link to clipboard

Copied

What is the format of these coordinates?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 18, 2017 0
New Here ,
Dec 18, 2017

Copy link to clipboard

Copied

Decimal Degrees

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 18, 2017 0
Adobe Community Professional ,
Dec 18, 2017

Copy link to clipboard

Copied

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

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 18, 2017 0
New Here ,
Dec 18, 2017

Copy link to clipboard

Copied

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

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 18, 2017 0
Adobe Community Professional ,
Dec 18, 2017

Copy link to clipboard

Copied

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)

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 18, 2017 0
New Here ,
Dec 18, 2017

Copy link to clipboard

Copied

Thank you much!

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 18, 2017 0
New Here ,
Dec 19, 2017

Copy link to clipboard

Copied

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?

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 19, 2017 0
New Here ,
Dec 19, 2017

Copy link to clipboard

Copied

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;

}

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 19, 2017 0
try67 LATEST
Most Valuable Participant ,
Dec 19, 2017

Copy link to clipboard

Copied

You can use something like this:

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

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

else event.value = dist;

Likes

Translate

Translate

Report

Report
Community Guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
Reply
Loading...
Dec 19, 2017 0