You can replace the ws.php into the previous Nearly1 example and you’ll get instant improvements and functionality. This will be the cornerstone of a few of the other iPhone Web Apps I’ll be releasing over the next few days.
Also, I found that to best test this code and make sure it was returning valid results, I added an option to render the results as KML. This is nice because you can then test it with Google Maps or Google Earth. Check out an example of this in action on Google Maps.
So, without further ado, I’ll jump into describing how the FlickrProxy works.
require_once("phpFlickr/phpFlickr.php");
// Create new phpFlickr object
$f = new phpFlickr("3055e96cbe8a5b33c248afb201bca822");
$f->enableCache("fs", "/home/bigyak/public_html/apps/nearly2/cache");
First, I’m using the phpFlickr API. There are good ways of installing it using PEAR (if you’re a bit more advanced of a PHP user), but it works just as well to copy the directory structure that I’ve provided. Enter in your Flickr API key, then put in a path to your cache directory (basically, just make a directory with write access turned on). You can comment that line out if you’re not using the cache.
if ($_GET["tags"]=="") {
$tags = "";
} else {
$tags = $_GET["tags"];
}
if ($_GET["daysago"]=="") {
if ($tags != "") {
//If they've entered tags, then don't be so restrictive on the date range
$daysago = 60;
} else {
$daysago = 7;
}
} else {
$daysago = $_GET["daysago"];
}
...
if ($_GET["format"]=="") {
$format = "json";
} else {
$format = $_GET["format"];
}
Next, we check all the variables that the user sent in through the querystring. If they put in tags to search on, we’ll look for those. If they entered a time range, we’ll search on that, then we’ll look via Latitude and Longitude as well as some other variables. For format, you can enter html, json, or kml. I’ve found that HTML is good for looking at quick results, KML is great for testing with Google Maps, and JSON is the type that our web apps can most easily use.
$RADIUS_EXPANSION = 10;
$DATE_EXPANSION = 4;
$PHOTOTRIES = 12;
$radius = 0.01;
for ($i = 1; $i <= $PHOTOTRIES; $i++) {
if($photos_found >= $MINPHOTOS) break;
$dateminformat = date("Y-m-d", mktime(0, 0, 0, date("m"), date("d")-$daysago, date("Y")));
$args = array("tags"=>$tags, "lat"=>$lat, "lon"=>$lon,"sort"=>"interestingness-desc", "per_page"=>$PHOTOTRIES, "radius"=>$radius, "min_taken_date"=>$dateminformat, "extras"=>"geo,date_taken,owner_name");
$photos_now = $f->photos_search($args);
This is the main loop of our code. We’re going to keep asking Flickr until we get the number of pictures back that the user has asked for, or until we’ve reached the 20mil limit of search. Notice that each successive query to Flickr is getting a little bigger and asking for older pictures. We’re ordering these by interestingness, but that doesn’t always return the most interesting pictures.
if(count($photos_now['photo']) > 0) {
foreach ($photos_now['photo'] as $photo) {
//Look through existing photos
$foundmatch=false;
foreach ($photos['photo'] as $lookingat) {
if ($photo[id]==$lookingat[id]) {
$foundmatch=true;
break;
}
}
if ($foundmatch==false) $photos['photo'][$photos_found++] = $photo;
}
$photos_found=count($photos['photo']);
The final part of the loop has us compare all returned pictures with our existing array of pictures. We throw away any duplicates and increment the count for checking later.
if ($format=="json") {
if(count($photos['photo']) == 0) {
if($backup=="yes"){
$html = file_get_contents('./test.json', true);
} else {
$html = 'jsonFlickrApi({"photos":"none", "stat":"ok", "requestedlat":"'.$lat.'", "requestedlon":"'.$lon.'"})'; }
} else {
$html = 'jsonFlickrApi({"photos":'.json_encode($photos).', "stat":"ok", "requestedlat":"'.$lat.'", "requestedlon":"'.$lon.'"})';;
}
...
I’ve found that sometimes you just don’t get any photos returned. As a last ditch effort, I have a backup file of json that I’ll just echo out if there are no results returned. For future versions, if none are returned in the a geo-area, I’ll experiment with increasign the geo box (rather than the radius).
If you look through the rest of the code, you’ll see the loops that manually build the KML or HTML for return to the users.
In summary, I’ve now improved the FlickrProxy to make it easier to ask for nearby images. For future work, I’ll be looking at how to further improve the iPhone interface to take advantage of HTML5.
(Back to last post in the series: Nearly1, the iPhone app)
(Next post in the series: Nearly2, iPhone app with 3D transforms)


We Create Games » Blog Archive » iPhone Geotags
June 18th, 2009
[...] (Next post in the series: An Updated Flickr Proxy) [...]
We Create Games » Blog Archive » 3d iPhone web apps
July 1st, 2009
[...] to last post in the series: FlickrProxy) (Next post in the series: Full Offline iPhone apps with HTML Canvas and [...]