2016-12-10 13 views
0

Google custom search apiは、画像結果の数を指定するのにnumというパラメータを使用すると言います.1から10までです。私はどのように10以上の画像とI found this articleを取得するために検索を繰り返す並べ替えの周りを見てきました。彼は言う:クエリの現在のインデックス位置を計算する方法は?

我々は最終的に 以上8つの結果を取得することができstart parameterを変更した場合、我々は、単一のクエリ内のサーバー から取得することができます8枚の写真の限界があるにもかかわらず。 start上に連結されたドキュメントから

戻す最初の結果のインデックスを意味します。

したがって、私は彼がクエリを実行するために使用したことを適用して自分のコードに適用しましたが、今は0の画像を取得します。

var myCx = "MY_CX"; 
var myKey = "MY_KEY"; 
var lastQueriedName; 
var termS = "hello"; 

// This bit is from the link which runs against the currentSTARTposition 

var currentSearchPosition = 0; 
function calculateStartPosition(termS, shift) { 
if (lastQueriedName === termS) { 
    currentSearchPosition += shift; 
    if (currentSearchPosition < 0) { 
     currentSearchPosition = 0; 
    } 
    if (currentSearchPosition >= 100) { 
     currentSearchPosition = 92; 
    } 
} else { 
    lastQueriedName = termS; 
    currentSearchPosition = 0; 
} 
    return currentSearchPosition; 
} 

// This is my own code which works only if 
// I don't insert the above function and I remove start: position 

$.getJSON("https://www.googleapis.com/customsearch/v1", { 
     q: termS, 
     alt: "json", 
     searchType: "image", 
     cx: myCx, 
     start: currentSearchPosition, 
     num: 10, 
     key: myKey, 
     rights: "cc_publicdomain", 
     filter: "1", 
     imgType: "photo", 
     fileType: "jpg" 
    }, 
    function (data) { 
     $.each(data.items, function(i,item){  
      $(".my_images .row").append('<div class="col-sm-4"><div class="thumbnail"><img class="img-responsive" src="' + item.link + '"></div></div>'); 
     }); 
    }); 
}; 

答えて

0

結局これは私が10の以上のイメージをつかん午前、私は、クエリを繰り返し、起動パラメータを使用する方法である:私は通話

function createGoogleImagesLoader(initialValue) { 
     var _start = initialValue || 1; 
     var imagesCount = 10; 

     return function() { 
      $.getJSON("https://www.googleapis.com/customsearch/v1", { 
       ... 
       num: imagesCount, 
       start: _start 
      },..); 
      _start += imagesCount; 

     } 
    } 

var googleImages = createGoogleImagesLoader(); 
googleImages() // load from 0 to 10 
googleImages() // load from 10 to 20 etc. 
間の開始値を保存
関連する問題