Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

How can I filter Bing Location API results by user location for auto complete?

I have this function to return bing address suggestions based off a user entered address. I'm already specifying userLocation, but the results are not nearly as accurate as I would imagine they should be. Is there anything extra I should be doing here? I can manually specify the state or postal code if I have to, but I'd rather avoid doing post processing.

For example, typing 111 returns results from 111, NY, 111 Sweden, 111 Denmark. I would like to restrict this to only the US at least, and really only within a few states which I can hardcode. When I manually specify the options for postalCode in the data, it overrides what I've used in query.

    var geolocate = function(options) {
    $.ajax({
        url: "http://dev.virtualearth.net/REST/v1/Locations",
        dataType: "jsonp",
        data: {
            key: "KEY",
            q: options.address,
            userLocation: options.latitude + ',' + options.longitude,
            maxResults: options.maxResults,
        },
        jsonp: "jsonp",
        success: function(data) {
            if (options.success instanceof Function) {
                options.success(data);
            }
        },
        error: function(xhr, status, text) {
            alert('ERROR:\n');
        }
    });
};
like image 427
AceoStar Avatar asked Nov 13 '22 14:11

AceoStar


1 Answers

As it turns out, this will actually do what I intended (Unless testing proves otherwise) I was specifying the address as a single param, q, but if I specify addressLine in combination with userLocation, it filters the results as expected.

var geolocate = function(options) {
    $.ajax({
        url: "http://dev.virtualearth.net/REST/v1/Locations",
        dataType: "jsonp",
        data: {
            key: "KEY",
            addressLine: options.address,
            countryRegion: 'US',
            userLocation: options.latitude + ',' + options.longitude,
            maxResults: options.maxResults,
        },
        jsonp: "jsonp",
        success: function(data) {
            if (options.success instanceof Function) {
                options.success(data);
            }
        },
        error: function(xhr, status, text) {
            alert('ERROR:\n\n');
        }
    });
};
like image 158
AceoStar Avatar answered Nov 15 '22 07:11

AceoStar