OpenLayers 3 Beginner’s Guide

Displaying photo information

Open sample in a new windows

<!doctype html>
    <title>Flickr Search</title>
    <link rel="stylesheet" href="../assets/ol3/css/ol.css" type="text/css" />
    <link rel="stylesheet" href="../assets/css/samples.css" type="text/css" />
    <div id="map" class="map"></div>
    <div id="photo-info"></div>
    <script src=""></script>
    <script src="../assets/ol3/js/ol-debug.js"></script>

    var flickrSource = new ol.source.Vector();

    var cache = {};

    function photoStyle(feature, scale) {
      var url = feature.get('url');
      var key = scale + url;
      if (!cache[key]) {
        cache[key] = new{
          image: new{
            scale: scale,
            src: url
      return cache[key];

    function flickrStyle(feature) {
      return [photoStyle(feature, 0.10)];

    function selectedStyle(feature) {
      return [photoStyle(feature, 0.50)];

    var flickrLayer = new ol.layer.Vector({
      source: flickrSource,
      style: flickrStyle

    var layer = new ol.layer.Tile({
      source: new ol.source.OSM()

    var center = ol.proj.transform([0,0], 'EPSG:4326', 'EPSG:3857');

    var view = new ol.View({
      center: center,
      zoom: 2

    var map = new ol.Map({
      target: 'map',
      layers: [layer, flickrLayer],
      view: view

    // a simple templating function that replaces keywords wrapped in
    // curly brackets with the equivalent value from the feature properties
    function photoContent(feature) {
      // get the HTML content of the template as a string
      var content = $('#photo-template').html();
      // a list of keys we will consider for replacement
      var keys = ['author','author_id','date_taken','latitude','longitude','link','url','tags','title'];
      for (var i=0; i<keys.length; i++) {
        var key = keys[i];
        // get the value of the key
        var value = feature.get(key);
        // and replace its template placeholder in the content. If the template
        // placeholder is absent, nothing happens so its safe
        content = content.replace('{'+key+'}',value);
      return content;

    var select = new ol.interaction.Select({
      layers: [flickrLayer],
      style: selectedStyle


    var selectedFeatures = select.getFeatures();

    selectedFeatures.on('add', function(event) {
      var feature =;
      // instead of the photo url, get the templated content
      // for the current feature instead
      var content = photoContent(feature);

    selectedFeatures.on('remove', function(event) {

    function successHandler(data) {
      var transform = ol.proj.getTransform('EPSG:4326', 'EPSG:3857');
      data.items.forEach(function(item) {
        var feature = new ol.Feature(item);
        var coordinate = transform([parseFloat(item.longitude), parseFloat(item.latitude)]);
        var geometry = new ol.geom.Point(coordinate);

      url: '../assets/data/flickr_data.json',
      dataType: 'jsonp',
      jsonpCallback: 'jsonFlickrFeed',
      success: successHandler


    <!-- script tags are ignored for rendering, and adding type="text/html"
         to the script tag makes the javascript engine ignore it, but
         we can still access it as a DOM element and get its formatted content -->
    <script type="text/html" id="photo-template">
      <a href="{link}" target="_blank" title="Click to open photo in new tab"><img src="{url}" style="float: left"></a><br>
      <p>Taken by <a href="{author_id}" target="_blank" title="Click to view author details in new tab">{author}</a> on {date_taken} at lat: {latitude} lon: {longitude}</p><br>
      <p>Tagged in <b>{tags}</b></p>