@@ -25,6 +25,9 @@ L.VectorGrid = L.GridLayer.extend({
2525 // A data structure holding initial symbolizer definitions for the vector features.
2626 vectorTileLayerStyles : { } ,
2727
28+ // Function that will execute per feature
29+ onEachFeature : null ,
30+
2831 // 🍂option interactive: Boolean = false
2932 // Whether this `VectorGrid` fires `Interactive Layer` events.
3033 interactive : false ,
@@ -41,27 +44,22 @@ L.VectorGrid = L.GridLayer.extend({
4144 if ( this . options . getFeatureId ) {
4245 this . _vectorTiles = { } ;
4346 this . _overriddenStyles = { } ;
44- this . on ( 'tileunload' , function ( e ) {
45- var key = this . _tileCoordsToKey ( e . coords ) ,
46- tile = this . _vectorTiles [ key ] ;
47-
48- if ( tile && this . _map ) {
49- tile . removeFrom ( this . _map ) ;
50- }
51- delete this . _vectorTiles [ key ] ;
52- } , this ) ;
5347 }
5448 this . _dataLayerNames = { } ;
49+ this . _userLayers = { } ;
50+ this . on ( 'tileunload' , function ( e ) {
51+ this . _tileUnload ( e ) ;
52+ } , this ) ;
5553 } ,
5654
5755 createTile : function ( coords , done ) {
58-
5956 var storeFeatures = this . options . getFeatureId ;
57+ var onEachFeature = this . options . onEachFeature ;
6058
6159 var tileSize = this . getTileSize ( ) ;
6260 var renderer = this . options . rendererFactory ( coords , tileSize , this . options ) ;
6361
64- var tileBounds = this . _tileCoordsToBounds ( coords ) ;
62+ var tileBounds = this . _tileCoordsToBounds ( coords ) ;
6563
6664 var vectorTilePromise = this . _getVectorTilePromise ( coords , tileBounds ) ;
6765
@@ -77,16 +75,16 @@ L.VectorGrid = L.GridLayer.extend({
7775 for ( var layerName in vectorTile . layers ) {
7876 this . _dataLayerNames [ layerName ] = true ;
7977 var layer = vectorTile . layers [ layerName ] ;
80-
78+
8179 var pxPerExtent = this . getTileSize ( ) . divideBy ( layer . extent ) ;
82-
80+
8381 var layerStyle = this . options . vectorTileLayerStyles [ layerName ] ||
8482 L . Path . prototype . options ;
85-
83+
8684 for ( var i = 0 ; i < layer . features . length ; i ++ ) {
8785 var feat = layer . features [ i ] ;
8886 var id ;
89-
87+
9088 var styleOptions = layerStyle ;
9189 if ( storeFeatures ) {
9290 id = this . options . getFeatureId ( feat ) ;
@@ -99,47 +97,53 @@ L.VectorGrid = L.GridLayer.extend({
9997 }
10098 }
10199 }
102-
100+
103101 if ( styleOptions instanceof Function ) {
104102 styleOptions = styleOptions ( feat . properties , coords . z ) ;
105103 }
106-
104+
107105 if ( ! ( styleOptions instanceof Array ) ) {
108106 styleOptions = [ styleOptions ] ;
109107 }
110-
108+
111109 if ( ! styleOptions . length ) {
110+ if ( onEachFeature ) {
111+ onEachFeature . call ( this , feat , null , layer , coords ) ;
112+ }
112113 continue ;
113114 }
114-
115+
115116 var featureLayer = this . _createLayer ( feat , pxPerExtent ) ;
116-
117+ if ( onEachFeature ) {
118+ onEachFeature . call ( this , feat , null , layer , coords ) ;
119+ }
120+
117121 for ( var j = 0 ; j < styleOptions . length ; j ++ ) {
118122 var style = L . extend ( { } , L . Path . prototype . options , styleOptions [ j ] ) ;
119123 featureLayer . render ( renderer , style ) ;
120124 renderer . _addPath ( featureLayer ) ;
121125 }
122-
126+
123127 if ( this . options . interactive ) {
124128 featureLayer . makeInteractive ( ) ;
125129 }
126-
130+
127131 if ( storeFeatures ) {
128132 renderer . _features [ id ] = {
129133 layerName : layerName ,
130134 feature : featureLayer
131135 } ;
132136 }
133137 }
134-
138+
135139 }
136-
140+
137141 }
138-
142+
139143 if ( this . _map != null ) {
140144 renderer . addTo ( this . _map ) ;
141145 }
142-
146+
143147 L . Util . requestAnimFrame ( done . bind ( coords , null , null ) ) ;
144148
145149 } . bind ( this ) , function ( err ) {
@@ -205,6 +209,47 @@ L.VectorGrid = L.GridLayer.extend({
205209 return Object . keys ( this . _dataLayerNames ) ;
206210 } ,
207211
212+ vtGeometryToPoint : function ( geometry , vtLayer , tileCoords ) {
213+ var pxPerExtent = this . getTileSize ( ) . x / vtLayer . extent ;
214+ var tileSize = this . getTileSize ( ) ;
215+ var offset = tileCoords . scaleBy ( tileSize ) ;
216+ var point ;
217+ if ( typeof geometry [ 0 ] === 'object' && 'x' in geometry [ 0 ] ) {
218+ // Protobuf vector tiles return [{x: , y:}]
219+ point = L . point ( offset . x + ( geometry [ 0 ] . x * pxPerExtent ) , offset . y + ( geometry [ 0 ] . y * pxPerExtent ) ) ;
220+ } else {
221+ // Geojson-vt returns [,]
222+ point = L . point ( offset . x + ( geometry [ 0 ] * pxPerExtent ) , offset . y + ( geometry [ 1 ] * pxPerExtent ) ) ;
223+ }
224+ return point ;
225+ } ,
226+
227+ vtGeometryToLatLng : function ( geometry , vtLayer , tileCoords ) {
228+ return this . _map . unproject ( this . vtGeometryToPoint ( geometry , vtLayer , tileCoords ) , tileCoords . z ) ;
229+ } ,
230+
231+ addUserLayer : function ( userLayer , tileCoords ) {
232+ var tileKey = this . _tileCoordsToKey ( tileCoords ) ;
233+ this . _userLayers [ tileKey ] = this . _userLayers [ tileKey ] || [ ] ;
234+ this . _userLayers [ tileKey ] . push ( userLayer ) ;
235+ this . _map . addLayer ( userLayer ) ;
236+ } ,
237+
238+ _tileUnload : function ( e ) {
239+ var tileKey = this . _tileCoordsToKey ( e . coords ) ;
240+ if ( this . _vectorTiles ) {
241+ delete this . _vectorTiles [ tileKey ] ;
242+ }
243+ var userLayers = this . _userLayers [ tileKey ] ;
244+ if ( ! userLayers ) {
245+ return ;
246+ }
247+ for ( var i = 0 ; i < userLayers . length ; i ++ ) {
248+ this . _map . removeLayer ( userLayers [ i ] ) ;
249+ }
250+ delete this . _userLayers [ tileKey ] ;
251+ } ,
252+
208253 _updateStyles : function ( feat , renderer , styleOptions ) {
209254 styleOptions = ( styleOptions instanceof Function ) ?
210255 styleOptions ( feat . properties , renderer . getCoord ( ) . z ) :
0 commit comments