0

さまざまな場所を表示するために Google Places API を使用しました。一度に 20 か所を表示したいのですが、ユーザーがテーブル ビューをスクロールして最後のフィールドに到達したら、残りのデータなどを追加したいと考えています。ビューを返し、1 つのことを除いて完全に機能する関数を作成しました。それ以上のデータが利用できない場合、すでにロードされている最後のデータをロードし続けます。これが私のコードです。

Ti.include('Functions/get_lat_long.js');
var myTable = Ti.UI.createTableView();
var next_page;
var nxt_pge_tkn;
var tableData = [];
function json_parsing(url,firsttime,winloading)
{

var view1=Ti.UI.createView({
        //height : '100%',
        width : '100%',
        backgroundColor : '#EDDA74',
        top : '10%',
        borderColor : "black"
    });
//For storing url in case next_page_token variable is invalid

var curloc=Ti.App.Properties.getString("curlocation");
//calling method in order to retrive latitude and longitude of current location
get_latitude_longitude(curloc);
//setting the base url that have been initialized in global.js file
var baseurl=Ti.App.Properties.getString("preurl");
//storing lat and lng file that have been initialized in get_lat_lon.js file get_latitude_longitude function 
var lat=Ti.App.Properties.getString("curlat");
var lng=Ti.App.Properties.getString("curlng");
//Storing radius which have been initialized in global.js file
var radiusmts=Ti.App.Properties.getInt("curradius")*1000;
//setting location type from the value that have been selected in app.js file by user
var loc_type=Ti.App.Properties.getString("curcategory");
//fetching and storing key which have been initialized in global.js file
var key=Ti.App.Properties.getString("apikey");
if(firsttime==true)
{
    winloading.open();
    var completeurl=baseurl+lat+","+lng+"&radius=" + radiusmts+ "&types=" + loc_type+ "&sensor=false&key=" + key;
}
else
{
    winloading.show();
    var completeurl=url;
}


var client = Ti.Network.createHTTPClient();


Ti.API.info("complete url " +completeurl);
client.open('GET',completeurl);
client.onload = function(e) {
       //For getting next_page_token so that next page results could be displayed
      var json = JSON.parse(this.responseText);

      if(json.next_page_token)
      {
        Ti.API.info("Next page token found ");
        next_page=true;
        nxt_pge_tkn=json.next_page_token;
      }
      else
      {     
        Ti.API.info("Next page token not found ");
        next_page=false;
      }

      if(json.results.length==0)
      {
        var lblno_record=Titanium.UI.createLabel({
            text : "No Record Found",
            color : "black",
            font : {fontSize : "25%" }
        });
      view1.add(lblno_record);
      }
      else
      {
      for(var i=0; i <json.results.length;i++) 
            {
            //Ti.API.info("Place " + json.results[i].name+ " Lat " + json.results[i].geometry.location.lat + " Lng " + json.results[i].geometry.location.lng);
            var row = Ti.UI.createTableViewRow({
                className : "row"
                //height : "80%"
                });
            //For temporary storing name in name variable
            var name=json.results[i].name;
            //Logic for shortening string in order to avoid overlapping of string
            (name.length>35)?name=name.substr(0,34)+ "..." :name=name;
            //Create label for displaying the name of place
            var lblname=Ti.UI.createLabel({
                //text : json.results[i].name,
                text :  name,
                color : "black",
                font : {fontSize : "20%"},
                left : "22%",
                top : "5%"
                });
             Ti.API.info("Name :- " + name);
            row.add(lblname);

            var add= json.results[i].vicinity;
            (add.length>125) ? add=add.substr(0,123)+ ". ." : add=add;

            var lbladdress=Ti.UI.createLabel({
                text : add,
                color : "black",
                font : {fontSize : "15%"},
                left : "22%",
                top : "30%",
                width : "71%"
                });
            row.add(lbladdress);

            var imgico=Ti.UI.createImageView({
                image : json.results[i].icon,
                height : "90",
                width : "90",
                left : "1%",
                top : "3%"
                //bottom : "10%"
                });
            row.add(imgico);      
            tableData.push(row);
            }
            //setting data that have been set to mytable view
            myTable.setData(tableData);
            view1.add(myTable);

            }
    winloading.hide();
};
client.onerror=function(e){
    alert("Network Not Avaliable");
};

myTable.addEventListener('scroll',function(e){
            var first=e.firstVisibleItem;
            var visible=e.visibleItemCount;
            var total=e.totalItemCount;
            Ti.API.info("Value of next_page_token before loop " + next_page);
            if(next_page==true &&  first+visible==total )
            {
                Ti.API.info("Value of next_page_token in loop " + next_page);
                var newurl="https://maps.googleapis.com/maps/api/place/nearbysearch/json?pagetoken="+nxt_pge_tkn+"&sensor=false&key="+key;
                firsttime=false;
                winloading.show();
                //myTable.removeEventListener('scroll',function(e){});
                json_parsing(newurl,firsttime,winloading);
                //get_next_page(newurl);
            }
        });



client.send();
return view1;
client.clearCookies();
}
4

1 に答える 1

0

私はあなたのコードを見ていましたが、指摘したいと思います:

  • ブロックには重要な問題があります。

    myTable.addEventListener('scroll',function(e){
    
    ...
    
    });
    

    このブロックは、json_parsing 関数を呼び出すたびに呼び出されます。そのため、myTable スクロール イベントにいくつかの関数が関連付けられます。これはあなたの意図ではないと確信しています。json_parsing の外に置く必要があります。

  • 特定の問題について、 client.onload 関数の json.next_page_token 値を確認してみてください。

    client.onload = function(e) {
    //For getting next_page_token so that next page results could be displayed
    var json = JSON.parse(this.responseText);
    Ti.API.info(JSON.stringify(this.responseText);
    if(json.next_page_token)
    {
    ...
    

    おそらく、値は空のオブジェクト {} またはスルーシー値を返す「false」文字列です。JavaScript には、false、undefined、null、0、''、および NaN の 6 つの偽値しかないことを忘れないでください。

  • 実際にはこれは小さな問題ですが、ドキュメントではHTTPClient.onloadおよび HTTPClient.onerror 関数は HTTPClient.open 関数を呼び出す前に設定する必要があります

  • ところで、あなたの json_parsing 関数の最後に到達不能なコードがありますが、あなたはすでにそれを知っていると思います:-)

    client.send();
    return view1;
    client.clearCookies(); //Unreachable code
    
于 2013-11-26T11:09:07.290 に答える