JQuery& Ajax - geonames自动完成并从foursquare请求信息

时间:2013-12-02 09:53:13

标签: jquery ajax foursquare geonames

我想找到我选择名称的城市(来自foursquare API)(来自geonames),但不幸的是它不起作用。我可以看到它从地理名称上传名称但是没有出现场地的结果。

<script type="text/javascript">
$(function() {
    function log( message ) {
        $( "<div>" ).text( message ).prependTo( "#log" );
        $( "#log" ).scrollTop( 0 );
    }
    $( "#muni" ).autocomplete({
        source: function( request, response ) {
            $.ajax({
                url: "http://ws.geonames.org/searchJSON",
                dataType: "jsonp",
                data: {
                    featureClass: "P",
                    style: "full",
                    maxRows: 15,
                    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.countryName
                        }
                    }));
                }    
            });
        },
        minLength: 2,
        select: function( event, ui ) {
            log( ui.item ?
                "Selected: " + ui.item.label :
                "Nothing selected, input was " + this.value);

            $.ajax({ 
                url : "https://api.foursquare.com/v2/venues/search?client_id=xxx&client_secret=xxx&v=20131212&format=JSONP&callback=x&limit=1&near=" + ui.item.value, 
                dataType : "jsonp",
                success : function(parsed_json) {
                    $("#name").text(parsed_json.response.venues.name);
                    $("#direccion").text(parsed_json['response']['venues']['location']['address']);
                    $("#postcode").text(parsed_json['response']['venues']['location']['postalCode']);
                    $("#city").text(parsed_json['response']['venues']['location']['city']);
                    $("#country").text(parsed_json['response']['venues']['location']['country']);           
        } 
            });                 


        },
        open: function() {
                $( this ).removeClass( "ui-corner-all" ).addClass( "ui-corner-top" );
        },
        close: function() {
                $( this ).removeClass( "ui-corner-top" ).addClass( "ui-corner-all" );
        }
    });
});

function getInfo(){
    $.ajax({ 
        url : "https://api.foursquare.com/v2/venues/search?client_id=xxx&client_secret=xxx&v=20131212&format=JSONP&callback=x&limit=1&near=" + $("#muni").val(), 
        dataType : "jsonp",
        success : function(parsed_json) {
            $("#name").text(parsed_json.response.venues.name);
            $("#direccion").text(parsed_json['response']['venues']['location']['address']);
            $("#postcode").text(parsed_json['response']['venues']['location']['postalCode']);
            $("#city").text(parsed_json['response']['venues']['location']['city']);
            $("#country").text(parsed_json['response']['venues']['location']['country']);           
        } 
    }); 
}


</script>

然后......

<input type="text" id="muni"></input>
<p onclick="getInfo()">Get all info about venue</p>

<table id="t">
    <tr>
        <th>Venue name</th>
        <th>Venue location-<br>address</th>
        <th>Venue location-<br>postcode</th>
        <th>Venue location-<br>city</th>
        <th>Venue location-<br>country</th>        
    </tr>
    <tr>
        <td id="name"></td>
        <td id="direccion"></td>
        <td id="postcode"></td>
        <td id="city"></td>
        <td id="country"></td>        
    </tr>
</table>

谢谢!

1 个答案:

答案 0 :(得分:0)

您没有正确解析。根据4square API中的测试控制台,返回的JSON中没有场地对象。这是我解析它的方式,它正在工作:

var index;
var obj = jQuery.parseJSON( parsed_json);

for(index = 0; index < obj.response.groups[0].items.length ; index++)
{
 console.log('id: ' + obj.response.groups[0].items[index].venue.id );
 console.log('Addr: ' + obj.response.groups[0].items[index].venue.location.address );
 console.log('ZIP Code: ' + obj.response.groups[0].items[index].venue.location.postalCode );
 console.log('City: ' + obj.response.groups[0].items[index].venue.location.city );
 console.log('Country: ' + obj.response.groups[0].items[index].venue.location.country );
}

希望有所帮助