Here's a very rough and indicative sketch of one approach (using miles (if you prefer km, or another planet, then you need to replace the value used for the planetary radius, and the final unit label).
If you get an NaN value, then the argument of the sleep command (waiting for the Google server to respond) will need to be increased.
JavaScript for Automation source
(() => {
'use strict';
// GENERIC ------------------------------------------------------------
// standardAdditions :: () -> Application
const standardAdditions = () =>
Object.assign(Application.currentApplication(), {
includeStandardAdditions: true
});
// HAVERSINE ---------------------------------------------------------
// haversine :: (lat :: Float, lng :: Float) ->
// (lat :: Float, lng :: Float) -> Float
const haversine = (dctFrom, dctTo) => {
// Math lib function names
const [
pi, asin, sin, cos, sqrt, pow, round
] = [
'PI', 'asin', 'sin', 'cos', 'sqrt', 'pow', 'round'
]
.map(k => Math[k]),
// degrees as radians
[rlat1, rlat2, rlon1, rlon2] = [
dctFrom.lat, dctTo.lat, dctFrom.lng, dctTo.lng
]
.map(x => x / 180 * pi),
dLat = rlat2 - rlat1,
dLon = rlon2 - rlon1,
radius = 3962.17; // radius of Earth in miles
// Adjust for other units or planets
// km
return round(
radius * 2 * asin(
sqrt(
pow(sin(dLat / 2), 2) +
pow(sin(dLon / 2), 2) *
cos(rlat1) * cos(rlat2)
)
) * 100
) / 100;
};
// MAIN --------------------------------------------------------------
const
kme = Application('Keyboard Maestro Engine'),
sa = standardAdditions(),
[dctFrom, dctTo] = ['fromAddress', 'toAddress']
.map(k => {
const
strURL = '"https://maps.googleapis.com/maps/api/' +
'geocode/json?address=' + kme.getvariable(k)
.split(/\s+/)
.join('+') + '"',
strJSON = sa.doShellScript('curl ' + strURL);
sa.doShellScript('sleep 1');
try {
return JSON.parse(strJSON)
.results[0].geometry.location
} catch (e) {
return {}
}
});
return '' + haversine(dctFrom, dctTo) + ' miles';
})();