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>');
});
});
};