首页 文章

Geonames得到纬度和经度

提问于
浏览
1

我一直在编写Google Geocoding代码,直到我发现地理编码API可能只与Google Map 一起使用 . 这很不方便,因为我不需要显示 Map . 所以我切换到Geonames,真棒!

我得到一个地址的纬度和经度,并将它们添加到我的数据库中 .

下面的代码是Jquery,用于文本输入,用户添加他们的城市/城镇,并且假设他们正在键入的城市出现自动完成 . 它工作得很好但我还想在2个隐藏的表单字段中添加他们城市的纬度和经度,以便发送到数据库 .

如何从Geonames中检索这些坐标?

$(function() {
        function log( message ) {
            $( "<div/>" ).text( message ).prependTo( "#log" );
            $( "#log" ).attr( "scrollTop", 0 );
        }

        $( "#city" ).autocomplete({
            source: function( request, response ) {
                $.ajax({
                    url: "http://ws.geonames.org/searchJSON",
                    dataType: "jsonp",
                    data: {
                        featureClass: "P",
                        style: "full",
                        maxRows: 12,
                        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
                            }
                        }));
                    }
                });
            },
            minLength: 2,
            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" );
            }
        });
    });

1 回答

相关问题