topical media & game development

talk show tell print

basic-ajax-07-Flickr.js / js



  var xmlhttp = null;
  var finalXML = null;
  
  if (window.ActiveXObject) 
  {
   finalXML = new ActiveXObject("MSXML2.DOMDocument.3.0");
  }
  else
  {
   finalXML = document.implementation.createDocument("","",null);
  }
  finalXML.appendChild(finalXML.createElement("markers"));
  
  countLength = 0;
  globalCount=0;
  var photoid = new Array();
  var phototitleid = new Array();
  var photoserverid = new Array();
  var photosecret = new Array();
  
                    
  function getRequest() 
  {
  if (window.XMLHttpRequest) 
  {
    return xmlhttp = new XMLHttpRequest(); 
  } 
      else if (window.ActiveXObject) 
      {  
      return xmlhttp = new ActiveXObject("Microsoft.XMLHTTP"); 
      } 
  }
  
  function sendData()
  {
       var request = getRequest();
      var url = "flickrproxy.aspx?method=flickr.people.getPublicPhotos&api_key=09c1df5594901a6b833cecbcc2974337&parametertype=user_id&parametervalue=" + document.getElementById("user").value;
      request.open("GET", url, true);
      request.onreadystatechange = function() { getData(request) };
      request.send(null);   
  }
  
  function getData() 
  {
   
    if ((xmlhttp.readyState == 4) &&( xmlhttp.status == 200))
    {
       
      var xmlobject = xmlhttp.responseXML;
      var photos =  xmlobject.getElementsByTagName("photo");
      countLength = photos.length;
      
      for (var i = 0; i < photos.length; i++) {
      photoid[i] = photos[i].getAttribute("id");
      phototitleid[i] = photos[i].getAttribute("title");
      photoserverid[i] = photos[i].getAttribute("server");
      photosecret[i] = photos[i].getAttribute("secret");
     } 
     sendGeoData();
     
    }
    
  }
    
    function sendGeoData()
    {
       
      var request = getRequest();   
      var url = "flickrproxy.aspx?method=flickr.photos.geo.getLocation&api_key=09c1df5594901a6b833cecbcc2974337&parametertype=photo_id&parametervalue=";
       
      request.open("GET", url + photoid[globalCount], true);
      request.onreadystatechange = function() { getGeoData(request) };
      request.send(null);
  
   }
  
  function getGeoData(request) 
  {
    if (request.readyState == 4 &&  request.status == 200)
    {
      xmlobject = request.responseXML;
      
      var newtag = finalXML.createElement("marker");
      var loc = xmlobject.getElementsByTagName("location")[0];
      var photo = xmlobject.getElementsByTagName("photo")[0];
      newtag.setAttribute("lat", loc.getAttribute("latitude"));
      newtag.setAttribute("lng", loc.getAttribute("longitude"));
      newtag.setAttribute("address", phototitleid[globalCount]);
      newtag.setAttribute("secret", photosecret[globalCount]);
      newtag.setAttribute("server", photoserverid[globalCount]);
      newtag.setAttribute("user_id", document.getElementById("user").value);
      newtag.setAttribute("photo_id", photo.getAttribute("id"));
      
      finalXML.firstChild.appendChild(newtag);
      
      globalCount++;
      
      if (globalCount != countLength)  {
          window.setTimeout(sendGeoData, 10);
      } else {
          saveXml();
      }
    }
  }
    
     
  function saveXml() 
  {
       var xmlToSave = null;
       if(window.ActiveXObject)
       {
          xmlToSave = finalXML.xml
       }
       else
       {
          xmlToSave = new XMLSerializer().serializeToString(finalXML);
       }
       var xmlcode = encodeURIComponent(xmlToSave);
       var url = "Save.aspx?xml=" + xmlcode;
       var request = getRequest();
       request.open("GET", url, true);
       request.onreadystatechange = function() {
            if (request.readyState == 4) {
                alert("File saved.");
            }
        };
        if (request.overrideMimeType) {
          request.overrideMimeType("text/plain");
        }
       request.send(null);
  }
  
  


(C) Æliens 20/2/2008

You may not copy or print any of this material without explicit permission of the author or the publisher. In case of other copyright issues, contact the author.