Search code examples
jquerybing-mapsbingazure-maps

I am using Bing maps API for autocomplete, i want to restrict the search for only user location (country)


I don't know how to restrict the autocomplete to a particular country?

I have also added Country code but that is not working as well. I want to get the user location and then want to perform autosuggest on the basis of the user country.

$(document).ready(function () {
        $("#to_destination").autocomplete({
            source: function (request, response) {
                $.ajax({
                    url:"https://dev.virtualearth.net/REST/v1/Locations",
                    dataType: "jsonp",
                    data: {
                        key: "API key",
                        q: request.term
                    },
                    jsonp: "jsonp",
                    success: function (data) {
                        var result = data.resourceSets[0];
                        if (result) {
                            if (result.estimatedTotal > 0) {
                                response($.map(result.resources, 
                                   function(item) {
                                    return {
                                        data: item,
                                        label: item.name + ' (' + 
                                  item.address.countryRegion + ')',
                                        value: item.name
                                    }
                                }));
                            }
                        }
                    }
                });
            },
            minLength: 1,
            change: function (event, ui) {
                if (!ui.item)
                    $("#to_destination").val('');
            },
            select: function (event, ui) {
                displaySelectedItem(ui.item.data);
            }
        })
    });

Solution

  • With Bing Maps the only option to try and limit the location API to a certain country is to add the country code to the query (i.e. query + ', ' + countryIsoCode). However this doesn't always work; CA is sometimes confused as California, other times the result is the country result and not for the query.