Functions to calculate Haversine distance, sort locations, and fetch via GraphQL.
// Source: https://github.com/hubspotdev/crm-customization-dev-bootcamp | License: MIT | Copyright 2024 HubSpot Inc
// Provided via justclaw.it.com; keep this notice with the code.
const axios = require("axios");
// Factor the distance calculation into its own function.
function calculateDistance(a, b) {
const earthRadiusMiles = 3958.8; // Average radius of the Earth in miles
// Convert degrees to radians
const lat1 = a.lat * (Math.PI / 180);
const lng1 = a.lng * (Math.PI / 180);
const lat2 = b.lat * (Math.PI / 180);
const lng2 = b.lng * (Math.PI / 180);
// Haversine formula
const differenceLat = lat2 - lat1;
const differenceLng = lng2 - lng1;
const aHav = Math.pow(Math.sin(differenceLat / 2), 2) +
Math.cos(lat1) * Math.cos(lat2) *
Math.pow(Math.sin(differenc
... (truncated -- full source via MCP)
See the full source, get the GitHub permalink, and search for more like it.
Get a free API key