Our offices

  • Gujarat
    35 Laxminarayan
    382305, Dahegam, Gandhinagar

Follow us

Integrating the Latest Version of Algolia InstantSearch with Google Maps

by Shreyansh Gohil, Founder / CEO

Integrating Algolia's InstantSearch with Google Maps can provide powerful search functionality combined with geographical visualization. This guide will walk you through the steps needed to set up this integration using React.

1. Install Package Dependencies

First, you need to install the required packages. Run the following command to install @vis.gl/react-google-maps,algoliasearch, and react-instantsearch.

npm install @vis.gl/react-google-maps algoliasearch react-instantsearch

2. Set Up Google Maps

Next, set up Google Maps in your React application. Use the following code to display a map centered at a specific position:

import { APIProvider, Map } from '@vis.gl/react-google-maps';
function App() {
  const position = { lat: 53.54992, lng: 10.00678 };
  return (
    <APIProvider apiKey={'YOUR API KEY HERE'}>
      <Map defaultCenter={position} defaultZoom={10}>
      </Map>
    </APIProvider>
  );
}
export default App;

3. Capture Map Bounds for Algolia Filtering

To filter Algolia search results based on the map’s visible area, capture the north-east and south-west coordinates whenever the map bounds change:

import { useState } from 'react';
import { APIProvider, Map } from '@vis.gl/react-google-maps';

function App() {
  const [state, setState] = useState(null);
  const [boundChanged, setBoundChanged] = useState(false);
  const position = { lat: 53.54992, lng: 10.00678 };

  return (
    <APIProvider apiKey={"YOUR API KEY HERE"}>
      <Map
        defaultCenter={position}
        defaultZoom={12}
        mapId={"6e120bcd575d29f7"}
        disableDefaultUI={true}
        onBoundsChanged={(data) => {
          const bounds = data.map.getBounds();
          setState({
            northEast: bounds.getNorthEast().toJSON(),
            southWest: bounds.getSouthWest().toJSON(),
          });
          setBoundChanged(true);
        }}
      ></Map>
    </APIProvider>
  );
}

export default App;

4. Add InstantSearch

Integrate Algolia InstantSearch into your application and set up the search client:

import { useState } from 'react';
import algoliasearch from 'algoliasearch';
import { InstantSearch } from 'react-instantsearch-dom';
import { APIProvider, Map } from '@vis.gl/react-google-maps';

const searchClient = algoliasearch(
  'ALGOLIA_APP_ID',
  'ALGOLIA_SEARCH_API_KEY'
);

function App() {
  const [state, setState] = useState(null);
  const [boundChanged, setBoundChanged] = useState(false);
  const position = { lat: 53.54992, lng: 10.00678 };

  return (
    <InstantSearch
      searchClient={searchClient}
      indexName={"indexName"}
      routing={true}
      future={{
        persistHierarchicalRootCount: true,
        preserveSharedStateOnUnmount: true,
      }}
    >
      <APIProvider apiKey={"YOUR API KEY HERE"}>
        <Map
          defaultCenter={position}
          defaultZoom={12}
          mapId={"6e120bcd575d29f7"}
          disableDefaultUI={true}
          onBoundsChanged={(data) => {
            const bounds = data.map.getBounds();
            setState({
              northEast: bounds.getNorthEast().toJSON(),
              southWest: bounds.getSouthWest().toJSON(),
            });
            setBoundChanged(true);
          }}
        >
        </Map>
      </APIProvider>
    </InstantSearch>
  );
}

export default App;

5. Fetch Items from Algolia

Finally, fetch items from Algolia and display them as markers on the map:

import { useState } from 'react';
import algoliasearch from 'algoliasearch';
import { InstantSearch, useGeoSearch } from 'react-instantsearch-dom';
import { APIProvider, Map } from '@vis.gl/react-google-maps';

const searchClient = algoliasearch(
  'ALGOLIA_APP_ID',
  'ALGOLIA_SEARCH_API_KEY'
);

function App() {
  const [state, setState] = useState(null);
  const [boundChanged, setBoundChanged] = useState(false);
  const position = { lat: 53.54992, lng: 10.00678 };
  const { items, refine } = useGeoSearch();

  return (
    <InstantSearch
      searchClient={searchClient}
      indexName={"indexName"}
      routing={true}
      future={{
        persistHierarchicalRootCount: true,
        preserveSharedStateOnUnmount: true,
      }}
    >
      <APIProvider apiKey={"YOUR API KEY HERE"}>
        <Map
          defaultCenter={position}
          defaultZoom={12}
          mapId={"6e120bcd575d29f7"}
          disableDefaultUI={true}
          onBoundsChanged={(data) => {
            const bounds = data.map.getBounds();
            setState({
              northEast: bounds.getNorthEast().toJSON(),
              southWest: bounds.getSouthWest().toJSON(),
            });
            setBoundChanged(true);
          }}
        >
          {items.map(single => {
            return single.map(position => {
              return (
                position && (
                  <Marker
                    position={position}
                  />
                )
              );
            });
          })}
        </Map>
      </APIProvider>
    </InstantSearch>
  );
}

export default App;

With these steps, you've successfully integrated Algolia InstantSearch with Google Maps in your React application. This setup will allow users to see search results visually on a map, enhancing their overall experience.

More articles

How to Use Shopify’s Storefront API for Headless Commerce

Explore the benefits of headless commerce with Shopify's Storefront API, enabling custom shopping experiences by decoupling the frontend from the backend.

Read more

Client-Side Error Handling with Slack Notifications

Discover how we tackled page break issues caused by incorrect data in Ghost CMS by creating a system that sends Slack alerts for broken pages. Learn how this solution helped one of our clients maintain seamless online course pages.

Read more

Tell us about your project

Our offices

  • Gujarat
    35 Laxminarayan
    382305, Dahegam, Gandhinagar