Last active
August 25, 2019 15:19
-
-
Save jibinpb/8fc1d191015ce085b71f3f5a110235b6 to your computer and use it in GitHub Desktop.
Location Autocomplete using Azure Maps & typeahead.js
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
<div id="remote"> | |
<input class="typeahead" type="text" placeholder="Type Here"> | |
</div> |
This file contains bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
// 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