Skip to content

Instantly share code, notes, and snippets.

@keyan1603
Created September 20, 2023 18:26
Show Gist options
  • Save keyan1603/60197526d38c6a5e7b9b48665097ee4b to your computer and use it in GitHub Desktop.
Save keyan1603/60197526d38c6a5e7b9b48665097ee4b to your computer and use it in GitHub Desktop.
Coveo distance sort using Javascript framework on static devices with no geolocation data
Coveo.$$(search).on("buildingQuery",
function (e, args) {
var currentLatitude = getCookie("currentlatitude");
var currentLongitude = getCookie("currentlongitude");
var latitudeField = '@@' + CoveoForSitecore.Context.fields.toCoveo('latitude');
var longitudeField = '@@' + CoveoForSitecore.Context.fields.toCoveo('longitude');
var distanceField = CoveoForSitecore.Context.fields.toCoveo('distance');
if (currentLatitude != '' && currentLongitude != '') {
var geoQuery = "$qf(function:'dist(" + latitudeField + "," + longitudeField + "," + currentLatitude + "," + currentLongitude + ")', fieldName: 'distance') ";
if (args && args.queryBuilder) {
args.queryBuilder.advancedExpression.add(geoQuery);
}
}
});
function getCookie(cname) {
let name = cname + "=";
let ca = document.cookie.split(';');
for (let i = 0; i < ca.length; i++) {
let c = ca[i];
while (c.charAt(0) == ' ') {
c = c.substring(1);
}
if (c.indexOf(name) == 0) {
return c.substring(name.length, c.length);
}
}
return "";
}
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment