hi team,
i would like to know is there any js for mobile which help us to calculate the given latitude & longitude to give the distance ?
Thanks,
Akash Gunupudi.
Yes, Akash, you can calculate the distance between two coordinates using JavaScript in a mobile application. A common mathematical approach for this is the Haversine Formula, which calculates the great-circle distance (straight-line distance over the Earth's surface) between two points based on their latitude and longitude.
Here's an example of the Haversine Formula implemented in JavaScript:
function calculateDistance(lat1, lon1, lat2, lon2) { const R = 6371; // Radius of the Earth in kilometers const dLat = (lat2 - lat1) * Math.PI / 180; // Convert degrees to radians const dLon = (lon2 - lon1) * Math.PI / 180; // Convert degrees to radians const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(lat1 * Math.PI / 180) * Math.cos(lat2 * Math.PI / 180) * Math.sin(dLon / 2) * Math.sin(dLon / 2); const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); const distance = R * c; // Distance in kilometers return distance; // Returns the distance in kilometers } // Example usage const distance = calculateDistance(28.7041, 77.1025, 12.9716, 77.5946); console.log("The distance in kilometers is: " + distance);
If you require more precise or route-specific distances, you can use Google's DistanceMatrix API, which provides the distance based on travel routes (e.g., driving, walking). This API is a paid offering and requires an API key from the Google Cloud Platform How can we calculate distance between two point on map in outsystems.
Let me know if you need further clarification!
he above code returns the result in kilometers (because you used Earth’s radius as 6371 km). If you need miles, you’d replace R with 3959.
Remember in above code it will return the direct displacement between 2 points not distance. If you need actual distance according to some path between 2 points you need to use some API as suggested.
Use the below script to get distance and also conversion in other units
function toRad(deg) {
return deg * Math.PI / 180;
}
function haversine(lat1, lon1, lat2, lon2) {
const R = 6371000; // Earth radius in meters
const dLat = toRad(lat2 - lat1);
const dLon = toRad(lon2 - lon1);
const a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
Math.sin(dLon / 2) * Math.sin(dLon / 2);
const c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
return R * c; // distance in meters
function convertDistance(meters, unit) {
switch (unit.toLowerCase()) {
case "km":
case "kilometer":
case "kilometers":
return meters / 1000; // meters → kilometers
case "mi":
case "mile":
case "miles":
return meters / 1609.344; // meters → miles
case "ft":
case "feet":
return meters * 3.28084; // meters → feet
default:
return meters; // default is meters
// ===== Usage inside OutSystems Run JavaScript =====
var meters = haversine($parameters.Lat1, $parameters.Lon1, $parameters.Lat2, $parameters.Lon2);
var result = convertDistance(meters, $parameters.Unit);
return result;
You can add it to JavaScript node and return the value to the output variable.