Skip to content

Instantly share code, notes, and snippets.

@mahboobi7
mahboobi7 / gist:499db76fdcd2b3ce6de6246fc126552f
Last active April 29, 2024 13:20
This GitHub Gist contains code snippets for implementing a feature in an ASP.NET Core Web API to find items within a specified radius using Entity Framework Core for database interaction.
private double HaversineDistance(Coordinates point1, Coordinates point2)
{
const double EarthRadiusKm = 6371;
var dLat = Math.PI * ((double)point2.Latitude - (double)point1.Latitude) / 180.0;
var dLon = Math.PI * ((double)point2.Longitude - (double)point1.Longitude) / 180.0;
var a = Math.Sin(dLat / 2) * Math.Sin(dLat / 2) +
Math.Cos(Math.PI * (double)point1.Latitude / 180.0) * Math.Cos(Math.PI * (double)point2.Latitude / 180.0) *
Math.Sin(dLon / 2) * Math.Sin(dLon / 2);