自动填充和Geonames:是否可以在单独的输入字段中检索城市,州和国家/地区?

时间:2011-10-18 02:11:03

标签: javascript jquery json autocomplete geonames

我目前正在使用单个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更改为其他options,但没有达到我的预期。

2 个答案:

答案 0 :(得分:1)

答案 1 :(得分:0)

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

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