Localities JS Widget - custom description

Localities JS Widget with custom description to easily embed a searchbar in your web page. This example shows how to customize the description of autocomplete suggestions.

  1. Example
  2. Running the Sample Locally

Example

Localities JS Widget - custom description
        function init(): void {
  const responseElement = document.getElementById(
    "response-container",
  ) as HTMLElement;

  // @ts-ignore
  const localitiesWidget = new window.woosmap.localities.Autocomplete(
    "autocomplete-input",
    {
      minLength: 3,
      components: {
        country: ["fr"],
      },
      customDescription:
        'postal_code:"{name} ({postal_town}) - {administrative_area_level_0}"',
    },
  );
  localitiesWidget.addListener("selected_suggestion", () => {
    const selectedLocality = localitiesWidget.getSelectedSuggestionDetails();
    if (responseElement) {
      responseElement.innerHTML = `<code>${JSON.stringify(selectedLocality, null, 2)}</code>`;
      responseElement.style.display = "block";
    }
  });
}

document.addEventListener("DOMContentLoaded", () => {
  init();
});

declare global {
  interface Window {
    // currently, the localities JS Widget typings are not exported, so we use `any` here
    //@ts-ignore
    woosmap: {
      localities: {
        Autocomplete: new (inputId: string, customConfig: any) => any;
      };
    };
  }
}

    
        function init() {
  const responseElement = document.getElementById("response-container");
  // @ts-ignore
  const localitiesWidget = new window.woosmap.localities.Autocomplete(
    "autocomplete-input",
    {
      minLength: 3,
      components: {
        country: ["fr"],
      },
      customDescription:
        'postal_code:"{name} ({postal_town}) - {administrative_area_level_0}"',
    },
  );

  localitiesWidget.addListener("selected_suggestion", () => {
    const selectedLocality = localitiesWidget.getSelectedSuggestionDetails();

    if (responseElement) {
      responseElement.innerHTML = `<code>${JSON.stringify(selectedLocality, null, 2)}</code>`;
      responseElement.style.display = "block";
    }
  });
}

document.addEventListener("DOMContentLoaded", () => {
  init();
});

    
        /*
 * Always set the map height explicitly to define the size of the div element
 * that contains the map.
 */
#map {
  height: 100%;
}

/*
 * Optional: Makes the sample page fill the window.
 */
html,
body {
  height: 100%;
  margin: 0;
  padding: 0;
  font-family: ui-sans-serif, system-ui, -apple-system, BlinkMacSystemFont, Segoe UI, Roboto, Helvetica Neue, Arial, Noto Sans, sans-serif, Apple Color Emoji, Segoe UI Emoji, Segoe UI Symbol, Noto Color Emoji;
}

body {
  background-color: #eee;
  overflow-y: hidden;
}

#autocomplete-container {
  display: flex;
  position: absolute;
  top: 10px;
  left: 10px;
  z-index: 1;
  box-shadow: 0 2px 4px rgba(0, 0, 0, 0.2), 0 -1px 0px rgba(0, 0, 0, 0.02);
  background: #fff;
  max-width: 320px;
  width: 100%;
  height: 42px;
  border: none;
  box-sizing: border-box;
  align-items: center;
  cursor: text;
  font-size: 15px;
}

#autocomplete-input {
  box-sizing: border-box;
  height: 40px;
  line-height: 24px;
  vertical-align: top;
  transition-property: color;
  transition-duration: 0.3s;
  width: 100%;
  padding: 0 12px;
  text-overflow: ellipsis;
  background: transparent;
  border-radius: 0;
  border: 0;
  margin: 0;
  outline: 0;
  overflow: visible;
  appearance: textfield;
  font-size: 100%;
}

.localities-input-wrapper, .localities-input-container {
  width: 100%;
}

pre {
  display: none;
  margin: 70px 10px;
  max-height: calc(100vh - 110px);
  padding: 10px;
  border: 3px solid #c8c8c8;
  background-color: white;
  border-radius: 8px;
  white-space: pre-wrap;
  overflow-x: auto;
  font-family: Consolas, "Andale Mono WT", "Andale Mono", "Lucida Console", "Lucida Sans Typewriter", "DejaVu Sans Mono", "Bitstream Vera Sans Mono", "Liberation Mono", "Nimbus Mono L", Monaco, "Courier New", Courier, monospace;
}


    
        <html>
  <head>
    <title>Localities JS Widget - custom description</title>
    <meta name="viewport" content="width=device-width, initial-scale=1.0" />
    <meta charset="utf-8" />

    <script src="https://sdk.woosmap.com/localities/localitieswidget.2.0.js?key=woos-48c80350-88aa-333e-835a-07f4b658a9a4"></script>
    <link
      rel="stylesheet"
      href="https://sdk.woosmap.com/localities/style.2.0.css"
    />

    <link rel="stylesheet" type="text/css" href="./style.css" />
    <script type="module" src="./index.js"></script>
  </head>
  <body>
    <div id="app">
      <div id="autocomplete-container">
        <input
          type="text"
          id="autocomplete-input"
          placeholder="Search a locality or a postal code..."
          autocomplete="off"
        />
      </div>
      <pre id="response-container"></pre>
    </div>
  </body>
</html>

    

Running the Sample Locally

Before you can run this sample on your local machine, you need to have Git and Node.js installed. If they’re not already installed, follow these instructions to get them set up.

Once you have Git and Node.js installed, you can run the sample by following these steps:

  1. Clone the repository and navigate to the directory of the sample.

  2. Install the necessary dependencies.

  3. Start running the sample.

Here are the commands you can use in your terminal to do this:

Shell
        git clone -b sample/localities-js-widget-custom-desc https://github.com/woosmap/js-samples.git
cd js-samples
npm i
npm start

    

You can experiment with other samples by switching to any branch that starts with the pattern sample/SAMPLE_NAME.

Shell
        git checkout sample/SAMPLE_NAME
npm i
npm start

    
Was this article helpful?
Have more questions? Submit a request