Localities Geocode
Sample code for Woosmap Map JavaScript API
author(s):
Woosmap
HTML
<!DOCTYPE html>
<html>
<head>
<title>Localities Geocode</title>
<meta name="viewport" content="width=device-width, initial-scale=1.0" />
<meta charset="utf-8" />
<!-- jsFiddle will insert css and js -->
</head>
<body>
<div id="app">
<div id="autocomplete-container">
<svg class="search-icon" viewBox="0 0 16 16">
<path
d="M3.617 7.083a4.338 4.338 0 1 1 8.676 0 4.338 4.338 0 0 1-8.676 0m4.338-5.838a5.838 5.838 0 1 0 2.162 11.262l2.278 2.279a1 1 0 0 0 1.415-1.414l-1.95-1.95A5.838 5.838 0 0 0 7.955 1.245"
fill-rule="evenodd"
clip-rule="evenodd"
></path>
</svg>
<input
type="text"
id="autocomplete-input"
placeholder="Geocode Localities..."
/>
<button aria-label="Clear" class="clear-searchButton" type="button">
<svg class="clear-icon" viewBox="0 0 24 24">
<path
d="M7.074 5.754a.933.933 0 1 0-1.32 1.317L10.693 12l-4.937 4.929a.931.931 0 1 0 1.319 1.317l4.938-4.93 4.937 4.93a.933.933 0 0 0 1.581-.662.93.93 0 0 0-.262-.655L13.331 12l4.937-4.929a.93.93 0 0 0-.663-1.578.93.93 0 0 0-.656.261l-4.938 4.93z"
></path>
</svg>
</button>
</div>
<aside id="response-panel" data-empty="true">
<div class="panel-header">
<div class="panel-title">Geocode response</div>
<div class="panel-hint">
Enter an address to geocode or click on the map to reverse geocode.
</div>
</div>
<div class="summary"></div>
<div class="address-components"></div>
<details class="raw-json-details">
<summary>
<span>Raw JSON</span>
<button
id="copy-raw-json"
class="copy-btn"
type="button"
title="Copy"
aria-label="Copy raw JSON"
>
<svg
class="icon-copy"
...
CSS
/*
* 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;
}
#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;
border-radius: 12px;
padding: 0 12px;
max-width: 320px;
width: 100%;
height: 42px;
border: none;
box-sizing: border-box;
align-items: center;
cursor: text;
font-size: 15px;
}
#autocomplete-container .search-icon, #autocomplete-container .clear-icon {
color: inherit;
flex-shrink: 0;
height: 16px;
width: 16px;
}
#autocomplete-container .clear-icon {
transform: scale(1.3);
}
#autocomplete-input {
box-sizing: border-box;
padding: 0;
height: 40px;
line-height: 24px;
vertical-align: top;
transition-property: color;
transition-duration: 0.3s;
width: 100%;
text-overflow: ellipsis;
background: transparent;
border-radius: 0;
border: 0;
margin: 0 8px;
outline: 0;
overflow: visible;
appearance: textfield;
font-size: 100%;
}
.clear-searchButton {
display: none;
height: 18px;
width: 22px;
background: none;
border: none;
vertical-align: middle;
pointer-events: all;
cursor: pointer;
}
#app {
height: 100%;
}
.btn {
background: #3d5afe;
border: 0;
border-radius: 4px;
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.14), 0 1px 5px 0 rgba(0, 0, 0, 0.12), 0 3px 1px -2px rgba(0, 0, 0, 0.2);
-webkit-transition: background 0.3s, color 0.3s;
transition: background 0.3s, color 0.3s;
box-sizing: border-box;
font-size: 14px;
color: #202124;
...
JavaScript
// Initialize and add the map
let map;
let marker;
let infoWindow;
let localitiesService;
const request = {};
function initMap() {
map = new window.woosmap.map.Map(document.getElementById("map"), {
center: { lat: 51.50940214, lng: -0.133012 },
zoom: 12,
});
infoWindow = new woosmap.map.InfoWindow({});
localitiesService = new woosmap.map.LocalitiesService();
map.addListener("click", (e) => {
handleGeocode(e.latlng);
});
}
const inputElement = document.getElementById("autocomplete-input");
const clearSearchBtn = document.getElementsByClassName("clear-searchButton")[0];
if (inputElement) {
inputElement.addEventListener("keydown", (e) => {
if (e.key === "Enter") {
handleGeocode(null);
}
});
inputElement.addEventListener("input", () => {
if (inputElement.value !== "") {
clearSearchBtn.style.display = "block";
} else {
clearSearchBtn.style.display = "none";
}
});
}
clearSearchBtn.addEventListener("click", () => {
inputElement.value = "";
clearSearchBtn.style.display = "none";
if (marker) {
marker.setMap(null);
infoWindow.close();
}
clearResponse();
inputElement.focus();
});
function handleGeocode(latlng) {
if (latlng) {
request.latLng = latlng;
delete request.address;
} else if (inputElement?.value !== "") {
request.address = inputElement.value;
delete request.latLng;
}
if (request.latLng || request.address) {
localitiesService
.geocode(request)
.then((localities) => displayLocality(localities.results[0]))
.catch((error) => console.error("Error geocoding localities:", error));
}
}
function displayLocality(locality) {
if (marker) {
marker.setMap(null);
infoWindow.close();
}
if (locality?.geometry) {
marker = new woosmap.map.Marker({
position: locality.geometry.location,
icon: {
url: "https://images.woosmap.com/marker.png",
scaledSize: {
height: 50,
width: 32,
...