Localities JS Widget
This example shows how to use the Localities JS Widget to easily embed a searchbar in your web page. The searchbar allows users to autocomplete for localities and retrieve details.
Example
Localities JS Widget
// The Localities JS Widget ships no typings: describe the parts this sample uses.
interface LocalitiesWidget {
addListener(eventName: string, handler: () => void): void;
getSelectedSuggestionDetails(): unknown;
}
interface LocalitiesWidgetGlobal {
localities: {
Autocomplete: new (inputId: string, options: object) => LocalitiesWidget;
};
}
function init(): void {
const responseElement = document.getElementById(
"response-container",
) as HTMLElement;
const { localities } = woosmap as unknown as LocalitiesWidgetGlobal;
const localitiesWidget = new localities.Autocomplete(
"autocomplete-input",
{},
);
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();
});
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 element
* that contains the map. `display: block` is required for <wmp-map>/<gmp-map>,
* which default to inline like any other custom element outside a flex/grid
* container (no-op for a plain <div>).
*/
#map {
display: block;
width: 100%;
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</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-e1e5490b-19d1-34f8-9955-fd5cfc8c880f"></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:
-
Clone the repository and navigate to the directory of the sample.
-
Install the necessary dependencies.
-
Start running the sample.
Here are the commands you can use in your terminal to do this:
git clone -b sample/localities-js-widget 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.
git checkout sample/SAMPLE_NAME
npm i
npm start