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