문제

I want the user to search for an address and i want it to show som examples and when the user chooses one the examples i want it to find the coordinates. But for now i can't get autocomplete to work at all and it won't search for addresses.

$('[id$=PlaceOfDeparture]:not(.ui-autocomplete-input)').live('focus', function () {
$(this).autocomplete({
    source: function (request, response) {
        $.ajax({
            url: "http://dev.virtualearth.net/REST/v1/Locations",
            dataType: "jsonp",
            data: {
                key: 'AvmdDLtsmPpOQ9N21vLDEAlhnr-H-W-A9HmjXiIDn9cHBVp5ylLELdc_lmnuCcRB',
                addressLine: request.term,
            },
            success: function (data) {
                var result = data;
            }
        });
    },
    minLength: 2,
    select: function (event, ui) {
        event.preventDefault();
        $(this).val(ui.item.label);
        travel = $(this).closest('div').parent();
        travel.find('[id$=PlaceOfDepartureCoordinates]').val(ui.item.value);
        travel.find('[id$=PlaceOfDepartureContry]').val(ui.item.countryName);
        $(this).change();
        updateMap();
    },
    open: function () {
        $(this).removeClass("ui-corner-all").addClass("ui-corner-top");
    },
    close: function () {
        $(this).removeClass("ui-corner-top").addClass("ui-corner-all");
    }
});

});

도움이 되었습니까?

해결책

You can find a working code sample of how to do this here: http://www.vivienchevallier.com/Articles/use-bing-maps-rest-services-with-jquery-to-build-an-autocomplete-box-and-find-a-location-dynamically

However, I highly recommend against doing this. Autocomplete usually generates a high volume of transactions against your account. If you are using an enterprise account this will result in high costs. If you are using a non-enterprise account you will run into issues where the auto complete will not work all the time as your account with be rate limited due to the high frequency of requests.

A much better approach to create the type of functionality you are looking for is to create a user ranked auto suggest. This will drastically improve the suggestions to the user and will make for a much better user experience while minimizing the amount of wasteful calls made to the Bing Maps service. The idea behind the user ranked auto suggest is to create a database where you can store the locations selected by your users. Every time a user selects a location in the auto suggest a rank value is increased and the ordering of the suggestions is based on the rank value. If the user does not find any results in the auto suggest that match their query, that's when they press the search button and you call the Bing Maps service to return possible results. If they select any of the results you would then add that result to your database. I have a couple of customers who have done this and after a few months they were hardly generating any transactions against Bing Maps which meant lower costs over the long term. It also meant that they had a lot of insight into what their users are looking for and which locations were the most popular. This kind of insight can be very valuable.

다른 팁

This is an very old post. Bing Maps now offers autosuggest. Here are some resources:
http://bingmapsv8samples.azurewebsites.net/#Fill%20Address%20Form%20with%20Autosuggest

http://bingmapsv8samples.azurewebsites.net/#Custom%20Autosuggest%20Input%20with%20JQuery%20UI

https://msdn.microsoft.com/en-us/library/mt750287.aspx

You may also want to consider Azure Maps which provides autosuggest for addresses, POI, and POI categories:

https://azure.com/maps

https://learn.microsoft.com/en-us/rest/api/maps/search/getsearchaddress

Simply use the typeahead URL parameter.

라이센스 : CC-BY-SA ~와 함께 속성
제휴하지 않습니다 StackOverflow
scroll top