Java for calculating the distance between two coordinates | XM Community
Skip to main content
Hello,



I have two coordinates that a respondent reported:

* A: 32.780491, -96.847283

* B: 32.764811, -96.831151



So, maybe

x1 = 32.780491

y1 = -96.847283

x2 = 32.764811

y2 = -96.831151



Are there any ways to calculate the distance between these two points, and record the value of distance (perhaps in miles) for Qualtrics embedded data?



Thank you very much in advance.
Hi @jhwang ,



You can use Math operation to do this.
Hi @Shashi,



I tried to calculate the distance between two coordinates, but I failed.



Here is my code:



Qualtrics.SurveyEngine.addOnReady(function()

{



var R = 6371;

var t1_dx = parseFloat("${e://Field/t1_dx}");

var t1_ox = parseFloat("${e://Field/t1_ox}");

var t1_dy = parseFloat("${e://Field/t1_dy}");

var t1_oy = parseFloat("${e://Field/t1_oy}");





var latDistance = Math.toRadians(t1_dx - t1_ox);

var lonDistance = Math.toRadians(t1_dy - t1_oy);

var a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)

+ Math.cos(Math.toRadians(t1_ox)) * Math.cos(Math.toRadians(t1_dx))

* Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

var distance = (R * c * 1000) / 1609.344; // convert to miles



var height = 0.00;



var distance2 = Math.pow(distance, 2) + Math.pow(height, 2);



var dist1 = Math.sqrt(distance2);



Qualtrics.SurveyEngine.setEmbeddedData("dist1", dist1);

});





Could you or anyone please correct my code?



Thank you very much.
Hi @jhwang



Please use the below code:



function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {

var R = 6371; // Radius of the earth in km

var dLat = deg2rad(lat2-lat1); // deg2rad below

var dLon = deg2rad(lon2-lon1);

var a =

Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *

Math.sin(dLon/2) * Math.sin(dLon/2)

;

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c; // Distance in km

return d;

}



function deg2rad(deg) {

return deg * (Math.PI/180)

}
@Shashi,



Thank you for your suggestion. But I still cannot see the embedded data value of the distance.



Here is my code:



Qualtrics.SurveyEngine.addOnReady(function()

{

var R = 6371;

var lat2 = Number("${e://Field/t1_dx}");

var lat1 = Number("${e://Field/t1_ox}");

var lon2 = Number("${e://Field/t1_dy}");

var lon1 = Number("${e://Field/t1_oy}");



function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {

var R = 6371; // Radius of the earth in km

var dLat = deg2rad(lat2-lat1); // deg2rad below

var dLon = deg2rad(lon2-lon1);

var a =

Math.sin(dLat/2) * Math.sin(dLat/2) +

Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *

Math.sin(dLon/2) * Math.sin(dLon/2)

;

var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

var d = R * c; // Distance in km

return d;

}



function deg2rad(deg) {

return deg * (Math.PI/180)

}



Qualtrics.SurveyEngine.setEmbeddedData("dist1", d);



});



Here is more detailed information of variables:

* Coordinates of origin: (`t1_ox`, `t1_oy`)

* Coordinates of destination: (`t1_dx`, `t1_dy`)



I checked if the embedded data of `t1_ox` ~ `t1_dy` work, and confirmed that they have no issues.



However, I cannot see the embedded data of `dist1`.



Thank you.
> @jhwang said:

> @Shashi,

>

> Thank you for your suggestion. But I still cannot see the embedded data value of the distance.

>

> Here is my code:

>

> Qualtrics.SurveyEngine.addOnReady(function()

> {

> var R = 6371;

> var lat2 = Number("${e://Field/t1_dx}");

> var lat1 = Number("${e://Field/t1_ox}");

> var lon2 = Number("${e://Field/t1_dy}");

> var lon1 = Number("${e://Field/t1_oy}");

>

> function getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) {

> var R = 6371; // Radius of the earth in km

> var dLat = deg2rad(lat2-lat1); // deg2rad below

> var dLon = deg2rad(lon2-lon1);

> var a =

> Math.sin(dLat/2) * Math.sin(dLat/2) +

> Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *

> Math.sin(dLon/2) * Math.sin(dLon/2)

> ;

> var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));

> var d = R * c; // Distance in km

> return d;

> }

>

> function deg2rad(deg) {

> return deg * (Math.PI/180)

> }

>

> Qualtrics.SurveyEngine.setEmbeddedData("dist1", getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) );

>

> });

>

> Here is more detailed information of variables:

> * Coordinates of origin: (`t1_ox`, `t1_oy`)

> * Coordinates of destination: (`t1_dx`, `t1_dy`)

>

> I checked if the embedded data of `t1_ox` ~ `t1_dy` work, and confirmed that they have no issues.

>

> However, I cannot see the embedded data of `dist1`.

>

> Thank you.



Change the last line to:



`Qualtrics.SurveyEngine.setEmbeddedData("dist1", getDistanceFromLatLonInKm(lat1,lon1,lat2,lon2) ); `



Hope you have created an embedded data dist1 in the survey flow before this question

Leave a Reply