Exit
  • Global community
    • Language:
      • Deutsch
      • English
      • Español
      • Français
      • Português
  • 日本語コミュニティ
  • 한국 커뮤니티
0

How to find distance between two coordinates using JavaScript

New Here ,
Dec 18, 2017 Dec 18, 2017

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
1.5K
Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2017 Dec 18, 2017

What is the format of these coordinates?

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

Decimal Degrees

Translate
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2017 Dec 18, 2017

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 18, 2017 Dec 18, 2017

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 18, 2017 Dec 18, 2017

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 18, 2017 Dec 18, 2017

Thank you much!

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

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
New Here ,
Dec 19, 2017 Dec 19, 2017

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
Report
Community guidelines
Be kind and respectful, give credit to the original source of content, and search for duplicates before posting. Learn more
community guidelines
Community Expert ,
Dec 19, 2017 Dec 19, 2017
LATEST

You can use something like this:

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

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

else event.value = dist;

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