OpenLayers 3 Beginner’s Guide

Getting dynamic data

Open sample in a new windows

<!doctype html>
<html>
  <head>
    <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" />
  </head>
  <body>
    <div id="map" class="map"></div>
    <div id="photo-info"></div>
    <script src="http://code.jquery.com/jquery-1.11.0.min.js"></script>
    <script src="../assets/ol3/js/ol-debug.js"></script>
    <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 ol.style.Style({
          image: new ol.style.Icon({
            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({
      renderer: 'canvas',
      target: 'map',
      layers: [layer, flickrLayer],
      view: view
    });

    function photoContent(feature) {
      var content = $('#photo-template').html();
      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];
        var value = feature.get(key);
        content = content.replace('{'+key+'}',value);
      }
      return content;
    }

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

    map.addInteraction(select);

    var selectedFeatures = select.getFeatures();

    selectedFeatures.on('add', function(event) {
      var feature = event.target.item(0);
      var content = photoContent(feature);
      $('#photo-info').html(content);
    });

    selectedFeatures.on('remove', function(event) {
      $('#photo-info').empty();
    });

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

    // the only change is to point at the remote URL for the feed
    $.ajax({
      url: 'http://api.flickr.com/services/feeds/geo/?format=json&tags=bird',
      dataType: 'jsonp',
      jsonpCallback: 'jsonFlickrFeed',
      success: successHandler
    });

    </script>

    <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="http://www.flickr.com/people/{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>
    </script>

  </body>
</html>