Is there a way to calculate the distance between two points that are not in a table?
for example I have a table with name, Longitude, Latitude I have a data table (component in ui builder)
that allows a user to select two rows and I’d like to calculate the distance between them.
I created points from the Longitude, Latitude of each selected row, but the only method I found to get the distance requires a query against a DB table
like this
distance( 30.26715, -97.74306, coordinates.latitude, coordinates.longitude ) < mi(200)
do I need to insert one point into a database table just to run a query against it?
I attempted to do this via the math functions but acos is not one of the available functions.
thanks for your time,
H
Hi @hharrington
Yes, it is possible.
Just use the CustomCode codeless block with the js code:
Regards,
Vlad
can you give me a link to a document/video describing how to pass arguments to and store a returned value from a customcode block? I’m having some difficulty finding it.
Thanks
H
Hi @hharrington
Not part of the team but this answers your question.
Create a function and paste the following code into raw codeless and it will give you what you looking for.
var Result, Co_ordiante_2, Co_ordiante_1;
function getObjectProperty(object, propPath) {
if (typeof propPath !== 'string' || object[propPath] !== undefined) {
return object[propPath]
}
const propsNamesList = propPath.split('.')
let result = object
for (let i = 0; i < propsNamesList.length; i++) {
if (!result || result[propsNamesList[i]] === undefined) {
return
}
result = result[propsNamesList[i]]
}
return result
}
Co_ordiante_1 = Point1;
Co_ordiante_2 = Point2;
Result = (await (async function(lat1, lat2, lon1, lon2) {
const R = 6371e3; // metres
const φ1 = lat1 * Math.PI/180; // φ, λ in radians
const φ2 = lat2 * Math.PI/180;
const Δφ = (lat2-lat1) * Math.PI/180;
const Δλ = (lon2-lon1) * Math.PI/180;
const a = Math.sin(Δφ/2) * Math.sin(Δφ/2) +
Math.cos(φ1) * Math.cos(φ2) *
Math.sin(Δλ/2) * Math.sin(Δλ/2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
const d = R * c; // in metres
return R * c
})((getObjectProperty(Co_ordiante_1, 'lat')), (getObjectProperty(Co_ordiante_2, 'lat')), (getObjectProperty(Co_ordiante_1, 'lng')), (getObjectProperty(Co_ordiante_2, 'lng'))));
return Result
Kind Regards,
Raymond
1 Like
In addition to the prev comments
configuring CustomCode is quite simple
Holy Cow! that worked! Thank you so much for posting your example!
you have no idea.