Here’s the ASP.NET code for this utility. Don’t forget to install the FlickrNet library from http://flickrnet.codeplex.com and set a reference to the .dll within the project.
using FlickrNet;
public partial class localpic : System.Web.UI.Page
{
private Location _Location; // Holds location information
// Load page, show initial pictures from city based on IP location services
protected void Page_Load(object sender, EventArgs e)
{
if (!IsPostBack)
{
_Location = new Location(Request);
// This class interacts with 3rd party library FlickrNet (http://flickrnet.codeplex.com/)
techyFlikr tf = new techyFlikr();
// Set city label and insert into dropdown if it doesn't already exist
if (_Location._oLocation.City.Length != 0)
{
lblCity.Text = _Location._oLocation.City;
}
ddlCities.DataSourceID = "dsCities";
ddlCities.DataBind();
if (!(ddlCities.Items.Contains(new ListItem(lblCity.Text))))
{
ddlCities.Items.Insert(0, _Location._oLocation.City.ToString());
}
ddlCities.Visible = true;
Session["loc"] = _Location;
// When page is loaded, display pictures from nearest geolocated city
if (_Location._oLocation.City.Length != 0)
{
gvPictures.DataSource = tf.getPhotos(_Location, ddlCities.SelectedValue.ToString()); ;
gvPictures.DataBind();
}
}
_Location = (Location)Session["loc"];
}
// User has selected a city, show pictures for item selected
protected void ddlCities_SelectedIndexChanged(object sender, EventArgs e)
{
techyFlikr tf = new techyFlikr();
// Get photos from Flickr service and bind to grid
gvPictures.DataSource = tf.getPhotos(_Location, ddlCities.SelectedValue.ToString()); ;
gvPictures.DataBind();
}
// User wants to see more pictures from selected city
protected void Button1_Click(object sender, EventArgs e)
{
techyFlikr tf = new techyFlikr();
gvPictures.DataSource = tf.getPhotos(_Location, ddlCities.SelectedValue.ToString()); ;
gvPictures.DataBind();
}
}
The code for the techyFlickr class is below:
public class techyFlikr
{
FlickrNet.Flickr _oFlikr = new FlickrNet.Flickr(" API goes here");
// Get pictures from service and return a List of Photos
public List getPhotos(Location loc, String sCity)
{
// Pick a random page between 1 and 40
Random rnd = new Random();
int page = rnd.Next(1, 40);
PhotoSearchOptions searchOptions = new PhotoSearchOptions();
searchOptions.Tags = sCity; // use the selected city as search tag
searchOptions.PerPage = 15;
searchOptions.Page = page;
if (sCity.Length == 0)
{
searchOptions.Tags = "New York"; // default to New York if no city found
}
// Perform the search and return collection of Photo objects
PhotoCollection photos = _oFlikr.PhotosSearch(searchOptions);
List allPhotos = new List();
allPhotos.AddRange(photos);
return allPhotos;
}
}
Here’s the class definitions for the Location service which shows how to deserialize a JSON response into a .NET object.
// Class to hold data from Response
public class myLocationJson
{
public string Ip { get; set; }
public string Status { get; set; }
public string CountryCode { get; set; }
public string RegionCode { get; set; }
public string RegionName { get; set; }
public string City { get; set; }
public string ZipPostalCode { get; set; }
public string Latitude { get; set; }
public string Longitude { get; set; }
}
public class Location
{
// Holds JSON data from location service
public myLocationJson _oLocation = new myLocationJson();
public string sMessage = string.Empty;
public Location(HttpRequest request)
{
try
{
// Get location from the IP browser address using a JSON web service
WebRequest myRequest = WebRequest.Create("http://ipinfodb.com/ip_query.php?ip=" +
request.UserHostAddress + "&output=json&timezone=false");
WebResponse myResponse = (WebResponse)myRequest.GetResponse();
String sLocation = string.Empty;
String sCity = string.Empty;
// Read web response into string
using (StreamReader sr = new StreamReader(myResponse.GetResponseStream()))
{ sLocation = sr.ReadToEnd(); }
// Read JSON response into our custom class to get properties
JavaScriptSerializer ser = new JavaScriptSerializer();
_oLocation = ser.Deserialize(sLocation);
}
catch (Exception ex)
{
sMessage = ex.Message;
_oLocation.Ip = string.Empty;
}
}
}
One thought on “Flickr Pictures”
Comments are closed.