1

我目前正在使用单个 INPUT 字段来触发 jQuery UI Autocomplete,它从 Geonames 中提取数据。

$( "#city_state_country" ).autocomplete({
    source: function( request, response ) {
        $.ajax({
            url: "http://ws.geonames.org/searchJSON",
            dataType: "jsonp",
            data: {
                featureClass: "P",
                style: "full",
                maxRows: 6,
                name_startsWith: request.term
            },
            success: function( data ) {
                response( $.map( data.geonames, function( item ) {
                    return {
                        label: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                        value: item.name + (item.adminName1 ? ", " + item.adminName1 : "") + ", " + item.countryName,
                    }
                }));
            }
        });
    },
    minLength: 3,
    select: function( event, ui ) {
        log( ui.item ?
            "Selected: " + ui.item.label :
            "Nothing selected, input was " + this.value);
    },
    open: function() {
        $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
    },
    close: function() {
        $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
    }
});

所以如果我开始输入“Bosto”

它为我提供

Boston, Massachusetts, United States

但我想为城市、州和国家提供单独的 INPUT 字段——并且它们每个都具有与 Geonames 挂钩的自动完成功能,分别只返回对城市、州、国家的建议。

所以输入:

"Bost" in the city INPUT would provide "Boston".
"Massa" in the state INPUT would provide "Massachusetts".
"United St" in the state INPUT would provide "United States".

这可能吗?

我已经尝试过更改name_startsWith为其他选项,但没有达到我的预期。

4

2 回答 2

1

根据文档http://www.geonames.org/export/web-services.html,你不能

于 2011-10-18T02:21:09.230 回答
1

您可以使用此 URL 来执行此操作 - 示例:api.geonames.org/postalCodeSearchJSON?placename_startsWith=dehi&country=LK&username=demo

它将为您提供的输入生成结果。

于 2016-01-19T06:39:53.783 回答