钛移动:在json解析中称为onload函数

时间:2012-02-10 10:22:16

标签: titanium-mobile

朋友你好,

我正在使用Google Place API在Titanium Studio sdk 1.8.1中开发一个应用程序来显示类别atm list& tableview中的地址所以我使用这个link 来使用json解析,但getData方法的loader.onload函数在json解析中的getData方法的send函数之后没有被立即调用,所以它在getDetailsData()函数之后被调用并且也无法在tableview中显示地址,所以请告诉我如何解决它。

提前致谢。

var lat ,lon ,radius , name , sensor , key , reference, address;
lat = '-33.8670522';//'23.042067';
lon = '151.1957362';//'72.530835';//
radius = '500';
name = title;
sensor = 'false';
key = 'AIzaSyDALrXHC4uMtfSrpCg6NHxqPhsLccLYPZE';

var rowData = [];

// getCategoryData using Google Place API
function getData()
{
    var loader = Titanium.Network.createHTTPClient();

    var url = "https://maps.googleapis.com/maps/api/place/search/json?";    
    url = url + "location=" + lat + ',' + lon;
    url = url + "&radius=" + radius;
    url = url + "&name=" + name;
    url = url + "&sensor=" + sensor;
    url = url + "&key=" + key;

    Ti.API.info(url);
    // Sets the HTTP request method, and the URL to get data from
    loader.open("GET",url);
    // Create our HTTP Client and name it "loader"
    // Runs the function when the data is ready for us to process
    loader.onload = function() 
    {
        var obj = JSON.parse(this.responseText);
        Ti.API.log(obj);    
        var results = obj.results;
        Ti.API.log(results);
        for (var i = 0; i < results.length; i++)
        {
            var name = obj.results[i].name; 
            reference = obj.results[i].reference;
            Ti.API.log('Refernce:'+reference);

                     getDetailsData();

            // Create a row and set its height to auto
            var row = Titanium.UI.createTableViewRow({height:'auto'});

            // Create the view that will contain the text and avatar
            var post_view = Titanium.UI.createView({
                height:'auto', 
                layout:'vertical',
                top:5,
                right:5,
                bottom:5,
                left:5
            });
                // Create the label to hold the tweet message
            var nameLabel = Titanium.UI.createLabel({
                //text:name,
                left:30,
                top:0,
                bottom:2,
                height:'auto',
                width:236,
                textAlign:'left',
                font:{fontSize:14}
            });

            // Create the label to hold the tweet message
            var addressLabel = Titanium.UI.createLabel({
                text:'Address',
                left:30,
                top:0,
                bottom:2,
                height:'auto',
                width:236,
                textAlign:'left',
                font:{fontSize:14}
            });

            nameLabel.text = name;
            //addressLabel.text = placeAddress;

            post_view.add(nameLabel);
            post_view.add(addressLabel);

            // Add the post view to the row
            row.add(post_view);
            // Give each row a class name
            //row.className = "item"+i;
            // Add row to the rowData array
            rowData[i] = row;
            //rowData.push(row);
        }

        //tableView.setData(rowData);
        // Create the table view and set its data source to "rowData" array
        var tableView = Titanium.UI.createTableView({data:rowData});
        //Add the table view to the window
        showWin.add(tableView);
    };
    //-- Network error
    loader.onerror = function(e)
    {
        Ti.API.info('Network error: ' + JSON.stringify(e));
    };


    // Send the HTTP request
    loader.send();
}




function getDetailsData () 
{
    var loader1 = Titanium.Network.createHTTPClient();

    Ti.API.log('getDetailsData');
    var url = "https://maps.googleapis.com/maps/api/place/details/json?";    
    url = url + "reference=" + reference;
    url = url + "&sensor=" + sensor;
    url = url + "&key=" + key;
    Ti.API.info(url);

    // Sets the HTTP request method, and the URL to get data from
    loader1.open("GET",url);

    // Runs the function when the data is ready for us to process
    loader1.onload = function() 
    {
        var detailsObj = JSON.parse(this.responseText);
        Ti.API.log(detailsObj); 

        address = detailsObj.result.formatted_address;
        Ti.API.log('Address:'+address);

        phoneno = detailsObj.result.formatted_phone_number;
        Ti.API.log('Phone No:'+phoneno);
    };

    //-- Network error
    loader1.onerror = function(event)
    {
        Ti.API.info('Network error: ' + JSON.stringify(event));
    };

    // Send the HTTP request
    loader1.send();

    return address;
}

getData();

enter image description here enter image description here

1 个答案:

答案 0 :(得分:1)

不要在第二个http请求中使用return。 将标签对象传递给函数,如:

  

getDetailsData(addressLabel);

并在 loader1.onload 中设置文字,如下所示:

  

address = detailsObj.result.formatted_address;

     

addressLabel.text = address;