Custom Java Setup - Calculate Distance Between Two Locations

Hi all,

I’m looking to generate the distance between the user’s current location and the distance of a specific location. Ultimately, I want this distance to be an app variable called distanceToLocation and display " (distanceToLocation output) Miles Away" where “123” is the display text of distanceToLocation.


I have given this a good attempt and but want to verify I’m doing this correctly and would love to hear everyone’s thoughts. Never done this before so any help is much appreciated. Currently I keep getting 0 miles away when I know there is certainly more distance between the two locations.

Step 1: I get the user’s current location coordinates by using the logic below in page layout and setting two app variables: userLatitude and userLongitude.

Step 2: I pull in the location’s latitude and longitude from my backend as part of the “123 Miles Away” logic and set those values to app variables locationLatitude and locationLongitude

Step 3: Create custom Java.

The code I am using in my Java is here: Calculate Distance by Latitude and Longitude using JavaScript | GeoDataSource (thanks to the forum post Calculate dinstance between two location).


Step 4: set app variable distanceToLocation be the result of the custom Java function


Thanks everyone for all the help here.

Hi, I am doing this in my app. I think I have already posted a description of how I did it - I will try and find it and link it into the discussion.

My distance calculation.

Thank you very much Phil! Could you take some screenshots of the logic inside your nodes please? That would help me better replicate your setup. Also, this is on the page logic, correct?

Also, I was able to get somewhere by changing my Javascript code. However, my function returns “undefined”. How can I fix this?

@sierge I’d debug it by adding some console.log()s with the variables to see what is undefined here.

Another thing – your output value is called distance but you’re returning result, that might affect it.

Thank you, Mari. Will give this a shot. Is there a way for me to run the code within the custom java node to see the output of the console.log?


Yes, I have this on the page logic. I will try and get time to screenshot the individual nodes in the next few days. The only complicated one though is the one above, the rest are fairly basic.

Hi all - after many hours of trial and error I actually found a formula-based solution and will paste it here in case it is helpful to anyone in the future. For some reason my Javascript kept returning ‘undefined’, so I abandoned that entire logic in favor of something easier. I replicated the Haversine formula which calculates the distance between two points using coordinates (Haversine formula - Wikipedia). Note that “x” will have to be replaced with “*” in your formula and denotes a multiplication.

ACOS((SIN(Lat 1 x PI()/180) x SIN(Lat 2 x PI()/180)+COS(Lat 1 x PI()/180) x COS(Lat 2 x PI()/180) x COS(Long 2 x PI()/180-Long 1 x PI()/180))) x 3958.8

The final 3958.8 denotes the radius of earth in miles. If you want km then use radius of earth in km.

1 Like

Great to hear you got it solved with a formula! The console.logs placed in a JavaScript node will appear in the Debugger when run on a mobile device, and in the Chrome developer tools console when in web preview.

Hi Mari - this worked as well. Thank you so much for the help here.

Hi. Is there any way to reduce the decimal points or round off the resulting fgure?

Yes,a simple formula. x 1000 (for 3 digits), take the integer value of that, then divide by 1,000.