<script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
<script type="text/javascript">
jQuery(document).ready(function() {
var subscription_key = '{{ azure_maps_key }}';
var addresssGeocodeServiceUrlTemplate = '{{ azure_maps_url }}/search/address/json?typeahead=true&subscription-key={subscription-key}&api-version=1&query={query}&language={language}&countrySet={countrySet}&view=Auto&limit=10';
//Create a jQuery autocomplete UI widget.
jQuery("{{ id }}").autocomplete({
minLength: 3,
source: function (request, response) {
//Create a URL to the Azure Maps search service to perform the address search.
var requestUrl = addresssGeocodeServiceUrlTemplate.replace('{query}', encodeURIComponent(request.term))
.replace('{subscription-key}', subscription_key ?? '')
.replace('{language}', 'fr-FR')
.replace('{countrySet}', '{{ current_country|upper }}');
//Proces the request.
fetch(requestUrl, {
method: 'GET',
mode: 'cors',
headers: {
'Content-Type': 'application/json; charset=utf-8'
}
}).then(r => r.json()).then(data => {
response(data.results);
});
},
select: function (event, ui) {
//When a suggestion has been selected.
var selection = ui.item;
//Populate the address textbox values.
document.getElementById('{{ address }}').value = (selection.address.streetNumber ? (selection.address.streetNumber + ' ') : '') + (selection.address.streetName || '');
document.getElementById('{{ city }}').value = selection.address.municipality || '';
document.getElementById('{{ zipCode }}').value = selection.address.postalCode || '';
document.getElementById('{{ country }}').value = selection.address.countryCode || '';
}
}).autocomplete("instance")._renderItem = function (ul, item) {
//Format the displayed suggestion to show the formatted suggestion string.
var suggestionLabel = item.address.freeformAddress;
if (item.poi && item.poi.name) {
suggestionLabel = item.poi.name + ' (' + suggestionLabel + ')';
}
return jQuery("<li>")
.append("<a>" + suggestionLabel + "</a>")
.appendTo(ul);
};
});
</script>