Skip to content

Instantly share code, notes, and snippets.

@jibinpb
Last active August 25, 2019 15:19
Show Gist options
  • Save jibinpb/8fc1d191015ce085b71f3f5a110235b6 to your computer and use it in GitHub Desktop.
Save jibinpb/8fc1d191015ce085b71f3f5a110235b6 to your computer and use it in GitHub Desktop.
Location Autocomplete using Azure Maps & typeahead.js
<div id="remote">
<input class="typeahead" type="text" placeholder="Type Here">
</div>
// Replace with Azure Map Key
var azure_maps_key = "AZURE_MAP_KEY";
var azure_maps_typeahead = new Bloodhound({
datumTokenizer: Bloodhound.tokenizers.obj.whitespace('value'),
queryTokenizer: Bloodhound.tokenizers.whitespace,
remote: {
url: 'https://atlas.microsoft.com/search/address/json?subscription-key=' + azure_maps_key + '&api-version=1.0&query=%QUERY',
wildcard: '%QUERY',
filter: function (response) {
var locations = [];
$(response.results).each(function (index) {
locations.push({
id: $(this)[0].id,
display: [$(this)[0].address.freeformAddress, $(this)[0].address.country].join(", "),
country: $(this)[0].address.country,
state: $(this)[0].address.countrySubdivision
});
});
return locations;//response.countries;
}
}
});
$('#remote .typeahead').typeahead(null, {
name: 'azure-maps-typeahead',
display: 'display',
source: azure_maps_typeahead
});
Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment