1+ const photosSource = new ol . source . Vector ( ) ;
2+
13const layerOSM = new ol . layer . Tile ( {
24 source : new ol . source . OSM ( )
35} ) ;
6+ const layerThumbs = new ol . layer . Vector ( {
7+ source : photosSource ,
8+ style : thumbStyle ,
9+ } ) ;
410const map = new ol . Map ( {
511 target : 'map' ,
6- layers : [ layerOSM ] ,
12+ layers : [ layerOSM , layerThumbs ] ,
713 view : new ol . View ( {
814 center : ol . proj . transform ( [ 20 , 47 ] , 'EPSG:4326' , 'EPSG:3857' ) ,
915 zoom : 4
1016 } )
11- } ) ;
17+ } ) ;
18+
19+ const handleMapDataLoaded = items => {
20+ const transform = ol . proj . getTransform ( 'EPSG:4326' , 'EPSG:3857' ) ;
21+ items . forEach ( item => {
22+ const feature = new ol . Feature ( item ) ;
23+ feature . set ( 'url' , item . preview ) ;
24+ const coordinate = transform ( [ parseFloat ( item . lon ) , parseFloat ( item . lat ) ] ) ;
25+ feature . setGeometry ( new ol . geom . Point ( coordinate ) ) ;
26+ photosSource . addFeature ( feature ) ;
27+ } ) ;
28+ } ;
29+ handleMapDataLoaded ( [ {
30+ "preview" : "https://annimon.com/albums/screens/dsc_1337_ps_5996.jpg.250.png" ,
31+ "lat" : 47.765957 ,
32+ "lon" : 37.255646
33+ } ] ) ;
34+
35+ // -- icon styles --
36+ const cache = { } ;
37+
38+ function photoStyle ( feature , scale ) {
39+ const url = feature . get ( 'url' ) ;
40+ const key = `${ scale } ${ url } ` ;
41+ if ( ! cache [ key ] ) {
42+ cache [ key ] = new ol . style . Style ( {
43+ image : new ol . style . Icon ( { src : url , scale} )
44+ } ) ;
45+ }
46+ return cache [ key ] ;
47+ }
48+
49+ function thumbStyle ( feature , resolution ) {
50+ return [ photoStyle ( feature , clamp ( 0.2 , 0.1 / resolution , 1 ) ) ] ;
51+ }
52+
53+ function clamp ( min , value , max ) {
54+ if ( value < min ) return min ;
55+ if ( value > max ) return max ;
56+ return value ;
57+ }
0 commit comments