Использование Google Images API - PullRequest
       0

Использование Google Images API

2 голосов
/ 18 августа 2011

Я пытаюсь использовать Google Images API для поиска изображения и помещаю его в мой HTML-документ в качестве div.Это то, что я до сих пор, но, кажется, ничего не появляется.Это части из http://code.google.com/apis/imagesearch/v1/devguide.html. Я впервые использую API, поэтому я не уверен, что на самом деле происходит.

<html> 
  <head> 
    <title>Art Project FTW</title> 
  </head> 
  <body> 
  <br>
  <br>

    <form name="upload" method="post" action="parse_image.php" enctype="multipart/form-data"> 
      <input type="file" name="Image"><br>  
      <input type="submit" value="Upload Image"> 
    </form> 
    <script type="text/javascript" src="https://www.google.com/jsapi?key=xxx"></script>
    <script type="text/javascript" charset="utf-8">

google.load('search', '1');

function searchComplete(searcher) {
  // Check that we got results
  if (searcher.results && searcher.results.length > 0) {
    // Grab our content div, clear it.
    var contentDiv = document.getElementById('content');
    contentDiv.innerHTML = '';

    // Loop through our results, printing them to the page.
    var results = searcher.results;
    for (var i = 0; i < results.length; i++) {
      // For each result write it's title and image to the screen
      var result = results[i];
      var imgContainer = document.createElement('div');

      var title = document.createElement('h2');
      // We use titleNoFormatting so that no HTML tags are left in the title
      title.innerHTML = result.titleNoFormatting;

      var newImg = document.createElement('img');
      // There is also a result.url property which has the escaped version
      newImg.src = result.tbUrl;

      imgContainer.appendChild(title);
      imgContainer.appendChild(newImg);

      // Put our title + image in the content
      contentDiv.appendChild(imgContainer);
    }
  }
}

function onload() {
  // Our ImageSearch instance.
  var imageSearch = new google.search.ImageSearch();

  // Restrict to extra large images only
  imageSearch.setRestriction(google.search.ImageSearch.RESTRICT_IMAGESIZE,
                             google.search.ImageSearch.IMAGESIZE_MEDIUM);

  // Here we set a callback so that anytime a search is executed, it will call
  // the searchComplete function and pass it our ImageSearch searcher.
  // When a search completes, our ImageSearch object is automatically
  // populated with the results.
  imageSearch.setSearchCompleteCallback(this, searchComplete, [imageSearch]);

  // Find me a beautiful car.
  imageSearch.execute("Subaru STI");
}
google.setonloadCallback(onload);​

</script>

  </body>
</html>

Заранее спасибо!

Ответы [ 2 ]

1 голос
/ 02 апреля 2012

Это не может работать, потому что вы ищете HTMLElement с идентификатором = 'content', у вас нет ни одного элемента с таким идентификатором

0 голосов
/ 07 января 2012

Попробуйте поместить свои функции js в <head></head>

Добро пожаловать на сайт PullRequest, где вы можете задавать вопросы и получать ответы от других членов сообщества.
...