Signature
Import
About this widget
ThegeoSearch
widget lets you search for results based on their position within a specified area (a bounding box).
It also provides features such as “search on map interactions”.
The
geoSearch
widget doesn’t let you search around a central point
or within polygons.
If you want this, you need to build your own UI on top of the Algolia API.Requirements
- Your hit records must have a
_geoloc
attribute so they can be displayed on the map. - You must load the Google Maps library and pass a reference to the widget: the library doesn’t come with InstantSearch.js.
- You must explicitly set the map container’s
height
. For example:
CSS
Examples
JavaScript
Options
The CSS Selector or
HTMLElement
to insert the widget into.The reference to the global
window.google
object.JavaScript
By default, the map sets the zoom level based on the displayed markers (results).
However, after InstantSearch has applied a refinement, there may be no results.
When this happens, a specific zoom level is required to render the map.
JavaScript
By default, the map sets the position based on the displayed markers (results).
However, after InstantSearch has applied a refinement, there may be no results.
When this happens, a specific position is required to render the map.
JavaScript
The options forwarded to the Google Maps constructor.
JavaScript
The options for customizing the built-in Google Maps markers.
This is ignored when the
customHTMLMarker
is provided.
The object accepts multiple attributes:createOptions: function
. A function to create the options passed to the Google Maps marker. The function is called withitem
, which is thehit
tied to the marker.events: object
. An object that takes event types (such asclick
andmouseover
) as keys and listeners as values. The listener is called with an object that contains propertiesevent
,item
,marker
, andmap
.
JavaScript
The options for customizing the HTML marker.
InstantSearch.js provides an alternative to the built-in Google Maps markers.
You can use plain HTML to build your custom markers (see
templates.HTMLMarker
).
The customHTMLMarker
object accepts several attributes:createOptions: function
. A function to create the options passed to theHTMLMarker
. The only supported option isanchor
. It lets you shift the marker position from the center of the element.events: object
. An object that takes event types (such asclick
andmouseover
) as keys and listeners as values. The listener is called with an object that contains propertiesevent
,item
,marker
, andmap
.
JavaScript
If
true
, the map is used for refining the search.
Otherwise, it’s only for display purposes.JavaScript
If
true
, a button is displayed on the map to allow users to clear any refinements.JavaScript
If
true
, users can toggle the option enableRefineOnMapMove
directly from the map.JavaScript
If
true
, refine is triggered as you move the map.JavaScript
The CSS classes you can override:
root
. The widget’s root element.map
. The map element.control
. The control element.label
. The label for the control element.selectedLabel
. The control element’s selected label.input
. The control element’s input.redo
. The “Redo search” button.disabledRedo
. The disabled “Redo search” button.reset
. The “Reset refinement” button.
JavaScript
Templates
You can customize parts of a widget’s UI using the Templates API. Each template includes anhtml
function,
which you can use as a tagged template.
This function safely renders templates as HTML strings and works directly in the browser—no build step required.
For details, see Templating your UI.
The
html
function is available in InstantSearch.js version 4.46.0 or later.The template to use for the marker.
The template for the reset button.
The template for the toggle label.
The template for the redo label.
HTML output
HTML
Customize the UI with connectGeoSearch
If you want to create your own UI of the geoSearch
widget, you can use connectors.
To use connectGeoSearch
, you can import it with the declaration relevant to how you installed InstantSearch.js.
JavaScript
Create a render function
This rendering function is called before the first search (init
lifecycle step)
and each time results come back from Algolia (render
lifecycle step).
JavaScript
Rendering options
The following rendering option example code snippets use Leaflet to render the map. If you prefer not to use Leaflet, you can use another library (such as Google Maps or Mapbox).Hits that matched the search request.
JavaScript
The current search position, when applicable.
JavaScript
The search’s bounding box:
northEast: { lat: number, lng: number }
. The top right corner of the map view.southWest: { lat: number, lng: number }
. The bottom left corner of the map view.
Sets a bounding box to filter the results from the given map bounds. The function accepts an object with:
northEast: { lat: number, lng: number }
. The top right corner of the map view.southWest: { lat: number, lng: number }
. The bottom left corner of the map view.
The function to send
click
or conversion
events.
The view
event is automatically sent when this connector renders hits.
To learn more, see the insights
middleware.eventType: 'click' | 'conversion'
hit: Hit | Hit[]
eventName: string
JavaScript
Resets the current bounding box refinement.
Returns
true
if the current refinement is set with the map bounds.Toggles whether users can refine on map move.
Returns
true
if users can refine on map move.Indicates that the map has moved since the last refinement.
This function should be called on each map move.
It helps ensure the map is only re-rendered when it has moved.
Returns
true
if the map has moved since the last refinement.All original widget options forwarded to the render function.
Create and instantiate the custom widget
First, create your custom widgets using a rendering function. Then, instantiate them with parameters. There are two kinds of parameters you can pass:- Instance parameters. Predefined options that configure Algolia’s behavior.
- Custom parameters. Parameters you define to make the widget reusable and adaptable.
renderFunction
, both instance and custom parameters are accessible through connector.widgetParams
.
JavaScript
Instance options
If
true
, refine is triggered as you move the map.JavaScript
A function that receives the list of items before they are displayed.
It should return a new array with the same structure.
Use this to transform, filter, or reorder the items.The function also has access to the full
results
data,
including all standard response parameters
and parameters from the helper,
such as disjunctiveFacetsRefinements
.JavaScript