src/Resources/views/Registration/azure_maps.html.twig line 1

Open in your IDE?
  1. <script src="https://atlas.microsoft.com/sdk/javascript/mapcontrol/3/atlas.min.js"></script>
  2. <script type="text/javascript">
  3.     jQuery(document).ready(function() {
  4.         var subscription_key = '{{ azure_maps_key }}';
  5.         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';
  6.         //Create a jQuery autocomplete UI widget.
  7.         jQuery("{{ id }}").autocomplete({
  8.             minLength: 3,
  9.             source: function (request, response) {
  10.                 //Create a URL to the Azure Maps search service to perform the address search.
  11.                 var requestUrl = addresssGeocodeServiceUrlTemplate.replace('{query}', encodeURIComponent(request.term))
  12.                     .replace('{subscription-key}', subscription_key ?? '')
  13.                     .replace('{language}', 'fr-FR')
  14.                     .replace('{countrySet}', '{{ current_country|upper }}');
  15.                 //Proces the request.
  16.                 fetch(requestUrl, {
  17.                     method: 'GET',
  18.                     mode: 'cors',
  19.                     headers: {
  20.                         'Content-Type': 'application/json; charset=utf-8'
  21.                     }
  22.                 }).then(r => r.json()).then(data => {
  23.                     response(data.results);
  24.                 });
  25.             },
  26.             select: function (event, ui) {
  27.                 //When a suggestion has been selected.
  28.                 var selection = ui.item;
  29.                 //Populate the address textbox values.
  30.                 document.getElementById('{{ address }}').value = (selection.address.streetNumber ? (selection.address.streetNumber  + ' ') : '') + (selection.address.streetName || '');
  31.                 document.getElementById('{{ city }}').value = selection.address.municipality || '';
  32.                 document.getElementById('{{ zipCode }}').value = selection.address.postalCode || '';
  33.                 document.getElementById('{{ country }}').value = selection.address.countryCode || '';
  34.             }
  35.         }).autocomplete("instance")._renderItem = function (ul, item) {
  36.             //Format the displayed suggestion to show the formatted suggestion string.
  37.             var suggestionLabel = item.address.freeformAddress;
  38.             if (item.poi && item.poi.name) {
  39.                 suggestionLabel = item.poi.name + ' (' + suggestionLabel + ')';
  40.             }
  41.             return jQuery("<li>")
  42.                 .append("<a>" + suggestionLabel + "</a>")
  43.                 .appendTo(ul);
  44.         };
  45.     });
  46. </script>