forked from mozilla/popcorn-js
/
popcorn.openmap.js
executable file
·387 lines (365 loc) · 14.5 KB
/
popcorn.openmap.js
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
360
361
362
363
364
365
366
367
368
369
370
371
372
373
374
375
376
377
378
379
380
381
382
383
384
385
386
387
// PLUGIN: OPENMAP
( function ( Popcorn ) {
/**
* openmap popcorn plug-in
* Adds an OpenLayers map and open map tiles (OpenStreetMap [default], NASA WorldWind, or USGS Topographic)
* Based on the googlemap popcorn plug-in. No StreetView support
* 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 WorldWind / LandSat), or TERRAIN (USGS).
* The Stamen custom map types can also be used (http://maps.stamen.com): STAMEN-TONER,
* STAMEN-TERRAIN, or STAMEN-WATERCOLOR.
* -Zoom [optional] defaults to 2
* -Lat and Lng are the coordinates of the map if location is not named
* -Location is a name of a place to center the map, geocoded to coordinates using TinyGeocoder.com
* -Markers [optional] is an array of map marker objects, with the following properties:
* --Icon is the URL of a map marker image
* --Size [optional] is the radius in pixels of the scaled marker image (default is 14)
* --Text [optional] is the HTML content of the map marker -- if your popcorn instance is named 'popped', use <script>popped.currentTime(10);</script> to control the video
* --Lat and Lng are coordinates of the map marker if location is not specified
* --Location is a name of a place for the map marker, geocoded to coordinates using TinyGeocoder.com
* 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' )
.openmap({
start: 5,
end: 15,
type: 'ROADMAP',
target: 'map',
lat: 43.665429,
lng: -79.403323
})
*
*/
var newdiv,
i = 1;
function toggle( container, display ) {
if ( container.map ) {
container.map.div.style.display = display;
return;
}
setTimeout(function() {
toggle( container, display );
}, 10 );
}
Popcorn.plugin( "openmap", function( options ){
var newdiv,
centerlonlat,
projection,
displayProjection,
pointLayer,
selectControl,
popup,
isGeoReady,
target = document.getElementById( options.target );
// create a new div within the target div
// this is later passed on to the maps api
newdiv = document.createElement( "div" );
newdiv.id = "openmapdiv" + i;
newdiv.style.width = "100%";
newdiv.style.height = "100%";
i++;
target && target.appendChild( newdiv );
return {
/**
* @member openmap
* The setup function will be executed when the plug-in is instantiated
*/
_setup: function( options ) {
// insert openlayers api script once
if ( !window.OpenLayers ) {
Popcorn.getScript( "//openlayers.org/api/OpenLayers.js", function() {
Popcorn.getScript( "//maps.stamen.com/js/tile.stamen.js" );
} );
}
// callback function fires when the script is run
isGeoReady = function() {
if ( ! ( window.OpenLayers && window.OpenLayers.Layer.Stamen ) ) {
setTimeout(function() {
isGeoReady();
}, 50);
} else {
if ( options.location ) {
// set a dummy center at start
location = new OpenLayers.LonLat( 0, 0 );
// query TinyGeocoder and re-center in callback
Popcorn.getJSONP(
"//tinygeocoder.com/create-api.php?q=" + options.location + "&callback=jsonp",
function( latlng ) {
centerlonlat = new OpenLayers.LonLat( latlng[ 1 ], latlng[ 0 ] );
}
);
} else {
centerlonlat = new OpenLayers.LonLat( options.lng, options.lat );
}
options.type = options.type || "ROADMAP";
switch( options.type ) {
case "SATELLITE" :
// add NASA WorldWind / LANDSAT map
options.map = new OpenLayers.Map({
div: newdiv,
maxResolution: 0.28125,
tileSize: new OpenLayers.Size( 512, 512 )
});
var worldwind = new OpenLayers.Layer.WorldWind(
"LANDSAT",
"//worldwind25.arc.nasa.gov/tile/tile.aspx",
2.25, 4,
{ T: "105" }
);
options.map.addLayer( worldwind );
displayProjection = new OpenLayers.Projection( "EPSG:4326" );
projection = new OpenLayers.Projection( "EPSG:4326" );
break;
case "TERRAIN":
// add terrain map ( USGS )
displayProjection = new OpenLayers.Projection( "EPSG:4326" );
projection = new OpenLayers.Projection( "EPSG:4326" );
options.map = new OpenLayers.Map({
div: newdiv,
projection: projection
});
var relief = new OpenLayers.Layer.WMS(
"USGS Terraserver",
"//terraserver-usa.org/ogcmap.ashx?",
{ layers: "DRG" }
);
options.map.addLayer( relief );
break;
case "STAMEN-TONER":
case "STAMEN-WATERCOLOR":
case "STAMEN-TERRAIN":
var layerName = options.type.replace("STAMEN-", "").toLowerCase();
var sLayer = new OpenLayers.Layer.Stamen( layerName );
displayProjection = new OpenLayers.Projection( "EPSG:4326" );
projection = new OpenLayers.Projection( 'EPSG:900913' );
centerlonlat = centerlonlat.transform( displayProjection, projection );
options.map = new OpenLayers.Map( {
div: newdiv,
projection: projection,
displayProjection: displayProjection,
controls: [
new OpenLayers.Control.Navigation(),
new OpenLayers.Control.PanPanel(),
new OpenLayers.Control.ZoomPanel()
]
} );
options.map.addLayer( sLayer );
break;
default: /* case "ROADMAP": */
// add OpenStreetMap layer
projection = new OpenLayers.Projection( 'EPSG:900913' );
displayProjection = new OpenLayers.Projection( 'EPSG:4326' );
centerlonlat = centerlonlat.transform( displayProjection, projection );
options.map = new OpenLayers.Map({
div: newdiv,
projection: projection,
"displayProjection": displayProjection
});
var osm = new OpenLayers.Layer.OSM();
options.map.addLayer( osm );
break;
}
if ( options.map ) {
options.map.setCenter(centerlonlat, options.zoom || 10);
options.map.div.style.display = "none";
}
}
};
isGeoReady();
var isReady = function() {
// wait until OpenLayers has been loaded, and the start function is run, before adding map
if ( !options.map ) {
setTimeout(function() {
isReady();
}, 13 );
} else {
// default zoom is 2
options.zoom = options.zoom || 2;
// make sure options.zoom is a number
if ( options.zoom && typeof options.zoom !== "number" ) {
options.zoom = +options.zoom;
}
// reset the location and zoom just in case the user played with the map
options.map.setCenter( centerlonlat, options.zoom );
if ( options.markers ) {
var layerStyle = OpenLayers.Util.extend( {} , OpenLayers.Feature.Vector.style[ "default" ] ),
featureSelected = function( clickInfo ) {
clickedFeature = clickInfo.feature;
if ( !clickedFeature.attributes.text ) {
return;
}
popup = new OpenLayers.Popup.FramedCloud(
"featurePopup",
clickedFeature.geometry.getBounds().getCenterLonLat(),
new OpenLayers.Size( 120, 250 ),
clickedFeature.attributes.text,
null,
true,
function( closeInfo ) {
selectControl.unselect( this.feature );
}
);
clickedFeature.popup = popup;
popup.feature = clickedFeature;
options.map.addPopup( popup );
},
featureUnSelected = function( clickInfo ) {
feature = clickInfo.feature;
if ( feature.popup ) {
popup.feature = null;
options.map.removePopup( feature.popup );
feature.popup.destroy();
feature.popup = null;
}
},
gcThenPlotMarker = function( myMarker ) {
Popcorn.getJSONP(
"//tinygeocoder.com/create-api.php?q=" + myMarker.location + "&callback=jsonp",
function( latlng ) {
var myPoint = new OpenLayers.Geometry.Point( latlng[1], latlng[0] ).transform( displayProjection, projection ),
myPointStyle = OpenLayers.Util.extend( {}, layerStyle );
if ( !myMarker.size || isNaN( myMarker.size ) ) {
myMarker.size = 14;
}
myPointStyle.pointRadius = myMarker.size;
myPointStyle.graphicOpacity = 1;
myPointStyle.externalGraphic = myMarker.icon;
var myPointFeature = new OpenLayers.Feature.Vector( myPoint, null, myPointStyle );
if ( myMarker.text ) {
myPointFeature.attributes = {
text: myMarker.text
};
}
pointLayer.addFeatures( [ myPointFeature ] );
}
);
};
pointLayer = new OpenLayers.Layer.Vector( "Point Layer", { style: layerStyle } );
options.map.addLayer( pointLayer );
for ( var m = 0, l = options.markers.length; m < l ; m++ ) {
var myMarker = options.markers[ m ];
if( myMarker.text ){
if( !selectControl ){
selectControl = new OpenLayers.Control.SelectFeature( pointLayer );
options.map.addControl( selectControl );
selectControl.activate();
pointLayer.events.on({
"featureselected": featureSelected,
"featureunselected": featureUnSelected
});
}
}
if ( myMarker.location ) {
var geocodeThenPlotMarker = gcThenPlotMarker;
geocodeThenPlotMarker( myMarker );
} else {
var myPoint = new OpenLayers.Geometry.Point( myMarker.lng, myMarker.lat ).transform( displayProjection, projection ),
myPointStyle = OpenLayers.Util.extend( {}, layerStyle );
if ( !myMarker.size || isNaN( myMarker.size ) ) {
myMarker.size = 14;
}
myPointStyle.pointRadius = myMarker.size;
myPointStyle.graphicOpacity = 1;
myPointStyle.externalGraphic = myMarker.icon;
var myPointFeature = new OpenLayers.Feature.Vector( myPoint, null, myPointStyle );
if ( myMarker.text ) {
myPointFeature.attributes = {
text: myMarker.text
};
}
pointLayer.addFeatures( [ myPointFeature ] );
}
}
}
}
};
isReady();
},
/**
* @member openmap
* 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 ) {
toggle( options, "block" );
},
/**
* @member openmap
* 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 ) {
toggle( options, "none" );
},
_teardown: function( options ) {
target && target.removeChild( newdiv );
newdiv = map = centerlonlat = projection = displayProjection = pointLayer = selectControl = popup = null;
}
};
},
{
about:{
name: "Popcorn OpenMap Plugin",
version: "0.3",
author: "@mapmeld",
website: "mapadelsur.blogspot.com"
},
options:{
start: {
elem: "input",
type: "number",
label: "Start"
},
end: {
elem: "input",
type: "number",
label: "End"
},
target: "map-container",
type: {
elem: "select",
options: [ "ROADMAP", "SATELLITE", "TERRAIN" ],
label: "Map Type",
optional: true
},
zoom: {
elem: "input",
type: "number",
label: "Zoom",
"default": 2
},
lat: {
elem: "input",
type: "text",
label: "Lat",
optional: true
},
lng: {
elem: "input",
type: "text",
label: "Lng",
optional: true
},
location: {
elem: "input",
type: "text",
label: "Location",
"default": "Toronto, Ontario, Canada"
},
markers: {
elem: "input",
type: "text",
label: "List Markers",
optional: true
}
}
});
}) ( Popcorn );