11class Mapping {
2- #list = [ ] ;
3-
4- /** Creates new map using given marker images */
5- constructor ( image ) {
6- this . style = new ol . style . Style ( { image : new ol . style . Icon ( ( { src : image } ) ) } )
7- }
2+ #features = [ ] ;
3+ #markers = [ ] ;
84
95 /** Add marker and link at a given position */
10- mark ( link , lon , lat , name ) {
6+ mark ( link , lon , lat , name , uri ) {
7+ const $image = document . createElement ( 'img' ) ;
8+ $image . src = uri ;
9+ $image . alt = name ;
10+ $image . classList . add ( 'marker' ) ;
11+
12+ const overlay = new ol . Overlay ( {
13+ position : ol . proj . fromLonLat ( [ lon , lat ] ) ,
14+ positioning : 'center-center' ,
15+ element : $image ,
16+ stopEvent : false
17+ } ) ;
18+ this . #markers. push ( overlay ) ;
19+
1120 const marker = new ol . Feature ( {
1221 geometry : new ol . geom . Point ( ol . proj . fromLonLat ( [ lon , lat ] ) ) ,
1322 link : link ,
1423 name : name
1524 } ) ;
16- marker . setStyle ( this . style ) ;
17- this . #list . push ( marker ) ;
25+
26+ this . #features . push ( marker ) ;
1827 }
1928
2029 /** Escape input for use in HTML */
@@ -24,13 +33,17 @@ class Mapping {
2433
2534 /** Project this map on to a given DOM element */
2635 project ( $element ) {
27- const source = new ol . source . Vector ( { features : this . #list} ) ;
2836 const map = new ol . Map ( {
2937 target : $element ,
3038 layers : [ new ol . layer . Tile ( { source : new ol . source . OSM ( ) } ) ]
3139 } ) ;
32- map . addLayer ( new ol . layer . Vector ( { source : source } ) ) ;
33- map . getView ( ) . fit ( source . getExtent ( ) , { padding : [ 32 , 32 , 32 , 32 ] , minResolution : 30 } ) ;
40+ for ( const overlay of this . #markers) {
41+ map . addOverlay ( overlay ) ;
42+ }
43+
44+ const features = new ol . source . Vector ( { features : this . #features} ) ;
45+ map . addLayer ( new ol . layer . Vector ( { source : features } ) ) ;
46+ map . getView ( ) . fit ( features . getExtent ( ) , { padding : [ 32 , 32 , 32 , 32 ] , minResolution : 30 } ) ;
3447
3548 const $popup = $element . querySelector ( '.popup' ) ;
3649 map . on ( 'movestart' , event => {
0 commit comments