volume_up

A critical vulnerability was discovered in React Server Components (Next.js). Our systems remain protected but we advise to update packages to newest version. Learn More

AI OnAI Off

Episerver Find GeoLocation Distance

I am currently working on a project that we are filtering results based on the distance from a given location. e.g Searching for Bristol and it returns locations within a 10 mile radius.

Is there a way to get the distance value for how far away a location is from the search location? e.g Bristol, Bedminster geolocation is 1.76 miles away from Bristol geolocation

Thanks

#183250
Oct 09, 2017 17:58

Thanks for your post. This is what I have implemented so far, was just hoping that there was a way to get it from find itself, as you can sort them by distance.

#183490
Oct 16, 2017 13:44

Found this post while also searching for the same answer. There is now a GeoCoordinate class in the Episerver.Personalization namespace that has a GetDistanceTo method. Unfortunately the types are mismatched, but you can do the following (in long hand for clarity):

const double MetersToMilesRatio = 0.00062137;
var currentLocation = new GeoCoordinate(geo.Latitude, geo.Longitude);
var toLocation = new GeoCoordinate(x.Latitude, x.Longitude);
var distance = Math.Round(currentLocation.GetDistanceTo(toLocation) * MetersToMilesRatio, 3);
#276439
Mar 16, 2022 16:25

When performing a geo-distance search, you can use the .DistanceFrom() method to calculate and retrieve the distance from a given point. Here's a simplified example in C#:

 
var centerLocation = new GeoLocation(51.4545, -2.5879); // Bristol coordinates
var results = searchClient
    .Search<LocationPage>() // Your content type
    .Filter(x => x.Coordinates.WithinDistanceFrom(centerLocation, 10, DistanceUnit.Miles))
    .OrderBy(x => x.Coordinates.DistanceFrom(centerLocation))
    .Select(x => new
    {
        x.Name,
        Distance = x.Coordinates.DistanceFrom(centerLocation)
    })
    .GetResult();
#340870
Oct 30, 2025 6:27
* You are NOT allowed to include any hyperlinks in the post because your account hasn't associated to your company. User profile should be updated.