@@ -20,7 +20,7 @@ export var TemplatedImageLayer = L.Layer.extend({
2020 onAdd : function ( ) {
2121 this . _map . _addZoomLimit ( this ) ; //used to set the zoom limit of the map
2222 this . setZIndex ( this . options . zIndex ) ;
23- this . _onMoveEnd ( ) ;
23+ this . _onAdd ( ) ;
2424 } ,
2525 redraw : function ( ) {
2626 this . _onMoveEnd ( ) ;
@@ -33,26 +33,90 @@ export var TemplatedImageLayer = L.Layer.extend({
3333 }
3434 } ,
3535
36- _onMoveEnd : function ( ) {
37- let mapZoom = this . _map . getZoom ( ) ;
38- let mapBounds = M . pixelToPCRSBounds ( this . _map . getPixelBounds ( ) , mapZoom , this . _map . options . projection ) ;
39- this . isVisible = mapZoom <= this . zoomBounds . maxZoom && mapZoom >= this . zoomBounds . minZoom &&
40- this . extentBounds . overlaps ( mapBounds ) ;
41- if ( ! ( this . isVisible ) ) {
42- this . _clearLayer ( ) ;
43- return ;
44- }
45- var map = this . _map ,
46- loc = map . getPixelBounds ( ) . min . subtract ( map . getPixelOrigin ( ) ) ,
47- size = map . getSize ( ) ,
48- src = this . getImageUrl ( ) ,
49- overlayToRemove = this . _imageOverlay ;
50- this . _imageOverlay = M . imageOverlay ( src , loc , size , 0 , this . _container ) ;
51-
52- this . _imageOverlay . addTo ( map ) ;
53- if ( overlayToRemove ) {
54- this . _imageOverlay . on ( 'load error' , function ( ) { map . removeLayer ( overlayToRemove ) ; } ) ;
55- }
36+ _addImage : function ( bounds , zoom , loc ) {
37+ let map = this . _map ;
38+ let overlayToRemove = this . _imageOverlay ;
39+ let src = this . getImageUrl ( bounds , zoom ) ;
40+ let size = map . getSize ( ) ;
41+ this . _imageOverlay = M . imageOverlay ( src , loc , size , 0 , this . _container ) ;
42+ this . _imageOverlay . _step = this . _template . step ;
43+ this . _imageOverlay . addTo ( map ) ;
44+ if ( overlayToRemove ) {
45+ this . _imageOverlay . _overlayToRemove = overlayToRemove . _url ;
46+ this . _imageOverlay . on ( 'load error' , function ( ) { map . removeLayer ( overlayToRemove ) ; } ) ;
47+ }
48+ } ,
49+
50+ _scaleImage : function ( bounds , zoom ) {
51+ let obj = this ;
52+ setTimeout ( function ( ) {
53+ let step = obj . _template . step ;
54+ let steppedZoom = Math . floor ( zoom / step ) * step ;
55+ let scale = obj . _map . getZoomScale ( zoom , steppedZoom ) ;
56+ let translate = bounds . min . multiplyBy ( scale )
57+ . subtract ( obj . _map . _getNewPixelOrigin ( obj . _map . getCenter ( ) , zoom ) ) . round ( ) ;
58+ L . DomUtil . setTransform ( obj . _imageOverlay . _image , translate , scale ) ;
59+ } ) ;
60+ } ,
61+
62+ _onAdd : function ( ) {
63+ let zoom = this . _map . getZoom ( ) ;
64+ let steppedZoom = zoom ;
65+ let step = this . _template . step ;
66+
67+ if ( zoom % step !== 0 ) steppedZoom = Math . floor ( zoom / step ) * step ;
68+ let bounds = this . _map . getPixelBounds ( this . _map . getCenter ( ) , steppedZoom ) ;
69+ this . _addImage ( bounds , steppedZoom , L . point ( 0 , 0 ) ) ;
70+ this . _pixelOrigins = { } ;
71+ this . _pixelOrigins [ steppedZoom ] = bounds . min ;
72+ if ( zoom !== steppedZoom ) {
73+ this . _scaleImage ( bounds , zoom ) ;
74+ }
75+ } ,
76+
77+ _onMoveEnd : function ( e ) {
78+ let mapZoom = this . _map . getZoom ( ) ;
79+ let history = this . _map . options . mapEl . _history ;
80+ let current = history [ history . length - 1 ] ;
81+ let previous = history [ history . length - 2 ] ;
82+ if ( ! previous ) previous = current ;
83+ let step = this . _template . step ;
84+ let steppedZoom = Math . floor ( mapZoom / step ) * step ;
85+ let bounds = this . _map . getPixelBounds ( this . _map . getCenter ( ) , steppedZoom ) ;
86+ //Zooming from one step increment into a lower one
87+ if ( ( step !== "1" ) && ( ( mapZoom + 1 ) % step === 0 ) &&
88+ current . zoom === previous . zoom - 1 ) {
89+ this . _addImage ( bounds , steppedZoom , L . point ( 0 , 0 ) ) ;
90+ this . _scaleImage ( bounds , mapZoom ) ;
91+ //Zooming or panning within a step increment
92+ } else if ( e && mapZoom % step !== 0 ) {
93+ this . _imageOverlay . _overlayToRemove = this . _imageOverlay . _url ;
94+ if ( current . zoom !== previous . zoom ) {
95+ //Zoomed from within one step increment into another
96+ if ( steppedZoom !== Math . floor ( previous . zoom / step ) * step ) {
97+ this . _addImage ( bounds , steppedZoom , L . point ( 0 , 0 ) ) ;
98+ this . _pixelOrigins [ steppedZoom ] = bounds . min ;
99+ }
100+ this . _scaleImage ( bounds , mapZoom ) ;
101+ } else {
102+ let pixelOrigin = this . _pixelOrigins [ steppedZoom ] ;
103+ let loc = bounds . min . subtract ( pixelOrigin ) ;
104+ if ( this . getImageUrl ( bounds , steppedZoom ) === this . _imageOverlay . _url ) return ;
105+ this . _addImage ( bounds , steppedZoom , loc ) ;
106+ this . _scaleImage ( bounds , mapZoom ) ;
107+ }
108+ } else {
109+ let mapBounds = M . pixelToPCRSBounds ( this . _map . getPixelBounds ( ) , mapZoom , this . _map . options . projection ) ;
110+ this . isVisible = mapZoom <= this . zoomBounds . maxZoom && mapZoom >= this . zoomBounds . minZoom &&
111+ this . extentBounds . overlaps ( mapBounds ) ;
112+ if ( ! ( this . isVisible ) ) {
113+ this . _clearLayer ( ) ;
114+ return ;
115+ }
116+ var map = this . _map , loc = map . getPixelBounds ( ) . min . subtract ( map . getPixelOrigin ( ) ) ;
117+ this . _addImage ( map . getPixelBounds ( ) , mapZoom , loc ) ;
118+ this . _pixelOrigins [ mapZoom ] = map . getPixelOrigin ( ) ;
119+ }
56120 } ,
57121 setZIndex : function ( zIndex ) {
58122 this . options . zIndex = zIndex ;
@@ -70,14 +134,14 @@ export var TemplatedImageLayer = L.Layer.extend({
70134 map . _removeZoomLimit ( this ) ;
71135 this . _container = null ;
72136 } ,
73- getImageUrl : function ( ) {
137+ getImageUrl : function ( pixelBounds , zoom ) {
74138 var obj = { } ;
75139 obj [ this . options . extent . width ] = this . _map . getSize ( ) . x ;
76140 obj [ this . options . extent . height ] = this . _map . getSize ( ) . y ;
77- obj [ this . options . extent . bottom ] = this . _TCRSToPCRS ( this . _map . getPixelBounds ( ) . max , this . _map . getZoom ( ) ) . y ;
78- obj [ this . options . extent . left ] = this . _TCRSToPCRS ( this . _map . getPixelBounds ( ) . min , this . _map . getZoom ( ) ) . x ;
79- obj [ this . options . extent . top ] = this . _TCRSToPCRS ( this . _map . getPixelBounds ( ) . min , this . _map . getZoom ( ) ) . y ;
80- obj [ this . options . extent . right ] = this . _TCRSToPCRS ( this . _map . getPixelBounds ( ) . max , this . _map . getZoom ( ) ) . x ;
141+ obj [ this . options . extent . bottom ] = this . _TCRSToPCRS ( pixelBounds . max , zoom ) . y ;
142+ obj [ this . options . extent . left ] = this . _TCRSToPCRS ( pixelBounds . min , zoom ) . x ;
143+ obj [ this . options . extent . top ] = this . _TCRSToPCRS ( pixelBounds . min , zoom ) . y ;
144+ obj [ this . options . extent . right ] = this . _TCRSToPCRS ( pixelBounds . max , zoom ) . x ;
81145 // hidden and other variables that may be associated
82146 for ( var v in this . options . extent ) {
83147 if ( [ "width" , "height" , "left" , "right" , "top" , "bottom" ] . indexOf ( v ) < 0 ) {
0 commit comments