Autocomplete Stores Fallback Google AutocompleteService
by woosmap
HTML
<div class="autocomplete-input-container">
<div class="autocomplete-input">
<input id="my-input-autocomplete" placeholder="Autocomplete Stores Fallback Google..." autocomplete="off" role="combobox">
</div>
<ul class="autocomplete-results">
</ul>
</div>
<div id="map"></div>
<!-- Replace the value of the key parameter with your own API key. -->
<script src="https://maps.googleapis.com/maps/api/js?key=AIzaSyCkUOdZ5y7hMm0yrcCQoCvLwzdM6M8s5qk&libraries=places&callback"></script>
<img src="https://images.woosmap.com/icons/coffee.svg" style="display:none">
<img src="https://images.woosmap.com/icons/coffee-selected.svg" style="display:none">
<img src="https://images.woosmap.com/icons/locality.svg" style="display:none">
<img src="https://images.woosmap.com/icons/locality-selected.svg" style="display:none">
CSS
#map {
height: 100%;
}
html,
body {
height: 100%;
margin: 0;
padding: 0;
}
.autocomplete-input-container {
position: absolute;
z-index: 1;
width: 100%;
}
.autocomplete-input {
text-align: center;
}
#my-input-autocomplete {
box-shadow: 0 2px 2px 0 rgba(0, 0, 0, 0.16), 0 0 0 1px rgba(0, 0, 0, 0.08);
font-size: 15px;
border-radius: 3px;
border: 0;
margin-top: 10px;
width: 290px;
height: 40px;
text-overflow: ellipsis;
padding: 0 1em;
}
#my-input-autocomplete:focus {
outline: none;
}
.autocomplete-results {
margin: 0 auto;
right: 0;
left: 0;
position: absolute;
display: none;
background-color: white;
width: 320px;
padding: 0;
list-style-type: none;
margin: 0 auto;
border: 1px solid #d2d2d2;
border-top: 0;
box-sizing: border-box;
}
.autocomplete-item {
padding: 5px 5px 5px 35px;
height: 26px;
line-height: 26px;
border-top: 1px solid #d9d9d9;
position: relative;
white-space: nowrap;
text-overflow: ellipsis;
overflow: hidden;
}
.autocomplete-icon {
display: block;
position: absolute;
top: 7px;
bottom: 0;
left: 8px;
width: 20px;
height: 20px;
background-repeat: no-repeat;
background-position: center center;
}
.autocomplete-icon.icon-localities {
background-image: url(https://images.woosmap.com/icons/locality.svg);
}
.autocomplete-item:hover .autocomplete-icon.icon-localities {
background-image: url(https://images.woosmap.com/icons/locality-selected.svg);
}
.autocomplete-item:hover {
background-color: #f2f2f2;
cursor: pointer;
}
.autocomplete-icon.icon-store {
background-image: url(https://images.woosmap.com/icons/coffee.svg);
}
.autocomplete-item:hover .autocomplete-icon.icon-store {
background-image: url(https://images.woosmap.com/icons/coffee-selected.svg);
}
.autocomplete-results.google::after {
content: "";
padding: 1px 1px 1px 0;
height: 18px;
box-sizing: border-box;
text-align: right;
display: block;
background-image:...
JavaScript
class JSONResponse {
constructor(response) {
this.response = response;
}
json() {
return new Promise((resolve, reject) => {
resolve(JSON.parse(this.response));
})
}
}
window.esXHR = new XMLHttpRequest();
window.woosXHR = new XMLHttpRequest();
function buildQueryString(params) {
let queryStringParts = [];
for (let key in params) {
if (params.hasOwnProperty(key)) {
let value = params[key];
queryStringParts.push(encodeURIComponent(key) + '=' + encodeURIComponent(value));
}
}
return queryStringParts.join('&');
}
function nFetch(url, globalXHR) {
return new Promise((resolve, reject) => {
let xhr = globalXHR || new XMLHttpRequest();
xhr.abort();
xhr.open('GET', url);
xhr.onload = () => {
if (xhr.status >= 200 && xhr.status < 300) {
resolve(new JSONResponse(xhr.response));
} else {
reject(xhr.statusText);
}
};
xhr.onerror = () => reject(xhr.statusText);
xhr.send();
});
}
function debounce(func, wait, immediate) {
let timeout;
return function() {
let context = this,
args = arguments;
let later = function() {
timeout = null;
if (!immediate) func.apply(context, args);
};
let callNow = immediate && !timeout;
clearTimeout(timeout);
timeout = setTimeout(later, wait);
if (callNow) func.apply(context, args);
};
}
function searchStores(input) {
let args = {
key: 'woos-81a699ca-5082-3ffd-9f54-a684a4b82853',
query: 'name:"' + input + '" OR city:"' + input + '"',
stores_by_page: 5
};
return nFetch('https://api.woosmap.com/stores/search/?' + buildQueryString(args), window.esXHR).then(function(response) {
return response.json()
});
}
function initAutocomplete() {
let map = new google.maps.Map(document.getElementById('map'), {
center: {
lat: 48,
lng: 4
},
zoom: 4,
disableDefaultUI: true
});
// Create the search box and link it to the UI element.
...