]> khamidou — GitHub mirrors - popcorn-js.git/commitdiff
Uploading new open maps plugin for Popcorn.js video mashup tool
authorNick Doiron <ndoiron@mapmeld.com>
Tue, 1 Mar 2011 04:32:40 +0000 (23:32 -0500)
committerNick Doiron <ndoiron@mapmeld.com>
Tue, 1 Mar 2011 04:32:40 +0000 (23:32 -0500)
Uses OpenLayers, OpenStreetMap, and NASA LandSat / World Wind

Adds time-sensitive markers

popcorn.openmap.html [new file with mode: 0644]
popcorn.openmap.js [new file with mode: 0644]
popcorn.openmap.unit.html [new file with mode: 0644]
popcorn.openmap.unit.js [new file with mode: 0644]

diff --git a/popcorn.openmap.html b/popcorn.openmap.html
new file mode 100644 (file)
index 0000000..f68f799
--- /dev/null
@@ -0,0 +1,65 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn Open Map Plug-in Demo</title>
+  
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.openmap.js"></script>
+  <script>
+       this.popped="";
+    document.addEventListener('DOMContentLoaded', function () {
+      this.popped = Popcorn('#video')
+      .volume(0)
+      .play() 
+      .openmap({
+        start: 0, // seconds
+        end: 20, // seconds
+        type: 'ROADMAP',
+        target: 'map',
+        lat: 43.665429,
+        lng: -79.403323,
+        zoom: "10"
+      } )
+      .openmap({
+        start: 0, // seconds
+        end: 30, // seconds
+        type: 'SATELLITE',
+        target: 'map2',
+        lat: 42.358544,
+        lng: -71.05957,
+        zoom: 8,
+               markers: [
+                               {start:0,end:100000000000000,popcorn:this,lat:42.358,lng:-71.0595,icon:"http://google-maps-icons.googlecode.com/files/palace.png"}
+                       ]
+               });
+    }, false);
+  </script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn Open Map Plug-in Demo</h1>
+  <p> An OpenLayers + OpenStreetMap Map displaying Toronto, Ontario will appear at 0 seconds and disappear at 20 seconds.</p>
+  <p> An OpenLayers + OpenStreetMap Map displaying Boston will appear at 0 seconds and disappear at 30 seconds.</p>
+  <div>
+    <video id='video' 
+      controls 
+      width= '250px'
+      poster="../../test/poster.png"> 
+
+      <source id='mp4'
+        src="../../test/trailer.mp4"
+        type='video/mp4; codecs="avc1, mp4a"'> 
+
+      <source id='ogv'
+        src="../../test/trailer.ogv"
+        type='video/ogg; codecs="theora, vorbis"'> 
+
+      <p>Your user agent does not support the HTML5 Video element.</p> 
+
+    </video>
+  </div>
+  <div style="width:700px">
+    <div id="map" style="position:relative;float:left;width:300px;height:400px"><p>Locations in this video:</p></div>
+    <div id="map2" style="position:relative;float:left;width:300px;height:400px"><p>Locations in this video:</p></div>
+  </div>
+</body>
+</html>
diff --git a/popcorn.openmap.js b/popcorn.openmap.js
new file mode 100644 (file)
index 0000000..aade86f
--- /dev/null
@@ -0,0 +1,197 @@
+// PLUGIN: Open Map
+var openlayersCallback;
+(function (Popcorn) {
+  
+  /**
+   * openmap popcorn plug-in 
+   * Adds an OpenLayers + OpenStreetMap map to the target div centered on the location specified by the user
+   * Based on the googlemap popcorn plug-in, location option not available
+   * Options parameter will need a start, end, target, type, zoom, lat and lng
+   * -Start is the time that you want this plug-in to execute
+   * -End is the time that you want this plug-in to stop executing 
+   * -Target is the id of the DOM element that you want the map to appear in. This element must be in the DOM
+   * -Type [optional] either: ROADMAP (OpenStreetMap), SATELLITE (NASA LandSat)... Coming Soon: TERRAIN (USGS), HYBRID
+   * -Zoom [optional] defaults to 2
+   * -Lat and Lng: the coordinates of the map must be present if location is not specified.
+   * -Markers: array of marker objects to be added to the map at given times
+   *  Note: using location requires extra loading time, also not specifying both lat/lng and location will
+   * cause a JavaScript error. 
+   * @param {Object} options
+   * 
+   * Example:
+     var p = Popcorn('#video')
+        .googlemap({
+          start: 5, // seconds
+          end: 15, // seconds
+          type: 'ROADMAP',
+          target: 'map',
+          lat: 43.665429,
+          lng: -79.403323
+        } )
+   *
+   */
+  Popcorn.plugin( "openmap" , (function(){
+      
+    var newdiv, i = 1, _mapFired = false, _mapLoaded = false;
+    
+    return {
+      manifest: {
+        about:{
+          name: "Popcorn OpenMap Plugin",
+          version: "0.1",
+          author: "@mapmeld",
+          website: "mapuganda.blogspot.com"
+        },
+        options:{
+          start    : {elem:'input', type:'text', label:'In'},
+          end      : {elem:'input', type:'text', label:'Out'},
+          target   : 'map-container',
+          type     : {elem:'select', options:['ROADMAP','SATELLITE'], label:'Type'},
+          zoom     : {elem:'input', type:'text', label:'Zoom'},
+          lat      : {elem:'input', type:'text', label:'Lat'},
+          lng     : {elem:'input', type:'text', label:'Lng'},
+                 markers  : {elem:'input', type:'text', label:'List Markers'},
+          location : {elem:'input', type:'text', label:'Location'}
+        }
+      },
+      _setup : function( options ) {
+        // create a new div this way anything in the target div 
+        // this is later passed on to the maps api
+        options._newdiv               = document.createElement('div');
+        options._newdiv.id            = "actualmap"+i;
+        options._newdiv.style.width   = "100%";
+        options._newdiv.style.height  = "100%";
+        i++;
+        if (document.getElementById(options.target)) {
+          document.getElementById(options.target).appendChild(options._newdiv);
+        }
+        
+        // insert openlayers api script once
+        if (!_mapFired) {
+          _mapFired = true;
+          var loadScriptTime = (new Date).getTime();
+          var head = document.getElementsByTagName('head')[0];
+          var script = document.createElement('script');
+         
+          script.src = "http://openlayers.org/api/OpenLayers.js";
+                 script.onload = function() {
+                               _mapLoaded    = true;
+                       };
+          script.type = "text/javascript";
+          head.insertBefore( script, head.firstChild ); 
+        }
+        // callback function fires when the script is run
+        // geolocation is not possible, only give lat and lng properties
+        var isGeoReady = function() {
+          if ( !_mapLoaded ) {
+            setTimeout(function () {
+              isGeoReady();
+            }, 5);
+          } else {
+
+                 options._location = new OpenLayers.LonLat(options.lng, options.lat);
+
+                       if(options.type == "ROADMAP"){
+                               // add OpenStreetMap layer
+                               options._projection = new OpenLayers.Projection('EPSG:900913');
+                               options._displayProjection = new OpenLayers.Projection('EPSG:4326');
+                               options._location = options._location.transform(options._displayProjection, options._projection);
+                               options._map = new OpenLayers.Map({div: options._newdiv, projection: options._projection, 'displayProjection': options._displayProjection});
+                               var osm = new OpenLayers.Layer.OSM();
+                               options._map.addLayer(osm);
+                       }
+                       else if(options.type == "SATELLITE"){
+                               // add NASA WorldWind / LANDSAT map
+                               var ww2 = new OpenLayers.Layer.WorldWind( "LANDSAT",
+                "http://worldwind25.arc.nasa.gov/tile/tile.aspx", 2.25, 4,
+                {T:"105"});
+                               options._map = new OpenLayers.Map({div: options._newdiv, 'maxResolution': .28125, tileSize: new OpenLayers.Size(512, 512)});
+                               options._map.addLayer(ww2);
+                               options._displayProjection = new OpenLayers.Projection('EPSG:4326');
+                               options._projection = new OpenLayers.Projection('EPSG:4326');
+                       }
+                       options._map.div.style.display = 'none';
+          }
+        };
+        isGeoReady();
+      },
+      /**
+       * @member webpage 
+       * The start function will be executed when the currentTime 
+       * of the video  reaches the start time provided by the 
+       * options variable
+       */
+      start: function(event, options){
+        // wait until OpenLayers is loaded, then check repeatedly if markers need to be added/removed
+               
+        var isReady = function () {    
+          if (!options._map) {
+            setTimeout(function () {
+              isReady();
+            }, 13);
+          } else {
+            options._map.div.style.display = 'block';
+            // make sure options.zoom is a number
+            if ( options.zoom && typeof options.zoom !== "number" ) {
+              options.zoom = +options.zoom;
+            }
+            options.zoom = options.zoom || 2; // default to 2
+
+            // reset the location and zoom just in case the user played with the map
+            options._map.setCenter(options._location, options.zoom);
+                       if(options.markers){
+                               setInterval(function() {
+                                       for(var m=0;m<options.markers.length;m++){
+                                               var myMarker=options.markers[m];
+                                               if(myMarker.isOn){
+                                                       //check if marker should be removed
+                                                       if((myMarker.popcorn.popped.currentTime() < myMarker.start)||(myMarker.popcorn.popped.currentTime() > myMarker.end)){
+                                                               options._pointLayer.removeFeatures([myMarker.isOn]);
+                                                               myMarker.isOn=false;
+                                                       }
+                                               }
+                                               else{
+                                                       //check if marker should be added
+                                                       if((myMarker.popcorn.popped.currentTime() > myMarker.start)&&(myMarker.popcorn.popped.currentTime() < myMarker.end)){
+                                                               var layerStyle = OpenLayers.Util.extend({}, OpenLayers.Feature.Vector.style['default']);
+                                                               if(!options._pointLayer){
+                                                                       options._pointLayer = new OpenLayers.Layer.Vector("Point Layer",{style: layerStyle});
+                                                                       options._map.addLayer(options._pointLayer);
+                                                               }
+                                                               var myPoint = new OpenLayers.Geometry.Point(myMarker.lng,myMarker.lat).transform( options._displayProjection,options._projection);
+                                                               var myPointStyle = OpenLayers.Util.extend({},layerStyle);
+                                                                       myPointStyle.pointRadius = 18;
+                                                                       myPointStyle.graphicOpacity = 1;
+                                                                       myPointStyle.externalGraphic = myMarker.icon;
+                                                               var myPointFeature = new OpenLayers.Feature.Vector(myPoint,null,myPointStyle);
+                                                               options._pointLayer.addFeatures([myPointFeature]);
+                                                               myMarker.isOn=myPointFeature;
+                                                       }
+                                               }
+                                       }
+                               }, 300);
+                       }
+          }
+        };
+        
+        isReady();
+      },
+      /**
+       * @member webpage
+       * The end function will be executed when the currentTime 
+       * of the video  reaches the end time provided by the 
+       * options variable
+       */
+      end: function(event, options){
+        // if the map exists hide it do not delete the map just in 
+        // case the user seeks back to time b/w start and end
+        if (options._map) {
+          options._map.div.style.display = 'none';          
+        }
+      }
+      
+    };
+    
+  })());
+
+})( Popcorn );
diff --git a/popcorn.openmap.unit.html b/popcorn.openmap.unit.html
new file mode 100644 (file)
index 0000000..cf921ef
--- /dev/null
@@ -0,0 +1,44 @@
+<!DOCTYPE html>
+<html>
+<head>
+  <title>Popcorn API</title>
+  <link rel="stylesheet" href="../../test/qunit/qunit.css" type="text/css" media="screen">
+  <script src="../../test/qunit/qunit.js"></script>
+  <!-- 
+    do not move - this must be called immediately prior to 
+    popcorn-api-draft.js
+  -->
+  
+  <script src="../../popcorn.js"></script>
+  <script src="popcorn.googlemap.js"></script>
+
+  <script src="popcorn.googlemap.unit.js"></script>
+</head>
+<body>
+  <h1 id="qunit-header">Popcorn Open Map Plugin</h1>
+  <h2 id="qunit-banner"></h2>
+  <div id="qunit-testrunner-toolbar"></div>
+  <h2 id="qunit-userAgent"></h2>
+  <ol id="qunit-tests"></ol>
+  <div id="qunit-fixture">  </div>
+  
+  <video id='video' 
+    controls preload='none' 
+    width= '250px'
+    poster="../../test/poster.png"> 
+
+    <source id='mp4'
+      src="../../test/trailer.mp4"
+      type='video/mp4; codecs="avc1, mp4a"'> 
+
+    <source id='ogv'
+      src="../../test/trailer.ogv"
+      type='video/ogg; codecs="theora, vorbis"'> 
+
+    <p>Your user agent does not support the HTML5 Video element.</p> 
+
+  </video> 
+  <div id="map" style="position:relative;float:left;width:300px;height:400px"></div> 
+  <div id="map2" style="position:relative;float:left;width:300px;height:400px"></div>   
+</body>
+</html>
diff --git a/popcorn.openmap.unit.js b/popcorn.openmap.unit.js
new file mode 100644 (file)
index 0000000..90200db
--- /dev/null
@@ -0,0 +1,81 @@
+test("Popcorn Open Map Plugin", function () {
+  
+  var popped = Popcorn("#video"),
+      expects = 10, 
+      count = 0,
+      mapInterval,
+      mapInterval2,
+      mapInterval3,
+      mapInterval4;
+  
+  expect(expects);
+  
+  function plus() {
+    if ( ++count===expects) {
+      start();
+    }
+  }
+  
+  stop();
+
+  ok ('openmap' in popped, "openmap is a method of the popped instance");
+  plus();
+  
+  ok ( document.getElementById('map').innerHTML === "", "initially, there is nothing inside the map" );
+  plus();
+  
+  ok ( document.getElementById('map2').innerHTML === "", "initially, there is nothing inside the map2" );
+  plus();
+  
+  popped.openmap({
+        start: 0, // seconds
+        end: 5, // seconds
+        type: 'ROADMAP',
+        target: 'map',
+        lat: 43.665429,
+        lng: -79.403323,
+        zoom: 10
+      } )
+      .googlemap({
+        start: 0, // seconds
+        end: 5, // seconds
+        type: 'SATELLITE',
+        target: 'map2',
+        lat: 0,
+        lng: 0,
+        zoom: 4
+      } )
+    .volume(0)
+    .play();
+  
+  
+  mapInterval = setInterval( function() {
+    if( popped.currentTime() > 3 && popped.currentTime() <= 5 ) {
+      ok(OpenLayers, "OpenLayers is available");
+      plus();
+      ok (document.getElementById('actualmap1'), "First map is on the page" );
+      plus();
+      equals (document.getElementById('actualmap1').offsetParent.id, "map", "First map is inside the 'map' div" );
+      plus();
+      clearInterval( mapInterval );
+    }
+  }, 1000);
+  mapInterval2 = setInterval( function() {
+    if( popped.currentTime() > 3 && popped.currentTime() <= 5 ) {
+      ok (document.getElementById('actualmap2'), "Second map is on the page" );
+      plus();
+      equals (document.getElementById('actualmap2').offsetParent.id, "map2", "Second map is inside the 'map2' div" );
+      plus();
+      clearInterval( mapInterval2 );
+    }
+  }, 1000);
+  mapInterval3 = setInterval( function() {
+    if( popped.currentTime() > 5  ) {
+      ok (document.getElementById('actualmap2').style.display === "none" && 
+          document.getElementById('actualmap1').style.display === "none", "Both maps are no longer visible" );
+      plus();
+      clearInterval( mapInterval3 );
+    }
+  }, 1000);
+  
+});