Google custom search api says to use the parameter num
to specify the number of image results, this goes from 1 to 10 only. I have been looking around how to sort of repeat the search to get more than 10 images and I found this article. He says:
Even though there is a limit of 8 pictures we can get from the server in a single query, if we modify the
start parameter
we can ultimately retrieve more than 8 results.
From the docs linked above the start
means The index of the first result to return.
Therefore I have applied what he used to run a query and I have applied it to my own code but I get now 0 images.
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>');
});
});
};
Eventually this is how I am grabbing more than 10 images, i repeat the query and use the start parameter: I save the start value between calls
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.