THIS IS ARCHIVED DOCUMENTATION

Adding a Custom Geolocation Provider Using the Coveo for Sitecore Legacy Search UI Framework

Coveo for Sitecore 4.1 (November 2018)

The Distance Resources Component calculates the user position based on data from specific providers. You can also use your own geolocation service to provide the user position to the Coveo Distance components.

If you want the component to use your custom provider in priority, you need to put it first in the providers list (see Coveo Distance Resources Component Properties).

To insert a custom geolocation provider

  1. Create a new JavaScript file in your instance to add your custom provider code.
  2. Copy the following code and update it with the needed calls to the geolocation provider API to retrieve the latitude and longitude of the user and set the correct variable names.

    This code snippet calls the setDistance(lat, lon) function from the distance resources component which will set the calculated distance on each item after you retrieved this information from the geolocation provider.

     document.addEventListener("DOMContentLoaded", function() {
         var searchInterfaceId = CoveoForSitecore.componentsOptions.id;
         var searchInterfaceElement = Coveo.$$(document.getElementById(searchInterfaceId));
    
         searchInterfaceElement.on(Coveo.InitializationEvents.afterComponentsInitialization, function(data) {
             var distanceResource = Coveo.get(searchInterfaceElement.find(".CoveoDistanceResources"));
             // Insert your code here (that is, API calls to get the current latitude and longitude).
             distanceResource.setDistance(YOUR_LATITUDE_VALUE, YOUR_LONGITUDE_VALUE);
         });
     });
    

    When using jQuery, replace document.addEventListener("DOMContentLoaded", function() { with Coveo.$(function() {

    You must bind this JavaScript code to the event afterComponentsInitialization since the Distance Resource Component must be initialized and available before it can run.

  3. The distance field is now set for your items that have geolocation information.