【问题标题】:Geonames get latitude and longitude地名获得纬度和经度
【发布时间】:2011-08-18 21:02:38
【问题描述】:

我一直在使用 Google Geocoding 进行编码,直到我发现 Geocoding API 只能与 Google 地图一起使用。这很不方便,因为我不需要显示地图。所以我改用 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" );
            }
        });
    });

【问题讨论】:

    标签: php jquery api dictionary geonames


    【解决方案1】:

    lat & lng 正在返回;查看此测试请求:

    http://api.geonames.org/searchJSON?style=full&maxRows=12&name_startsWith=romse&username=demo

    它在下面的结构中,所以你只需要访问它:

    { "geonames" : [ { 
    "adminCode1" : "ENG",
    <SNIP>
            "geonameId" : 2639189,
            "lat" : 50.989057046475203,
            "lng" : -1.4998912811279297
    

    【讨论】:

    • 谢谢!!我不知道一切都回来了。我尝试将item.lat 放在成功函数中,它会检索纬度! :) 你知道有没有办法在select: function( event, ui ) { 中检索它?
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 2017-01-01
    • 1970-01-01
    • 1970-01-01
    • 2012-06-24
    • 2011-07-06
    • 1970-01-01
    • 2012-03-13
    相关资源
    最近更新 更多