Last active
April 29, 2024 13:20
-
-
Save mahboobi7/499db76fdcd2b3ce6de6246fc126552f to your computer and use it in GitHub Desktop.
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.
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
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); | |
var c = 2 * Math.Atan2(Math.Sqrt(a), Math.Sqrt(1 - a)); | |
var distance = EarthRadiusKm * c; | |
return distance; | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment