@@ -25,7 +25,9 @@ L.VectorGrid = L.GridLayer.extend({
2525 // A data structure holding initial symbolizer definitions for the vector features.
2626 vectorTileLayerStyles : { } ,
2727
28- // 🍂option interactive: Boolean = false
28+ onEachFeature : null ,
29+
30+ // 🍂option interactive: Boolean = false
2931 // Whether this `VectorGrid` fires `Interactive Layer` events.
3032 interactive : false ,
3133
@@ -41,21 +43,17 @@ L.VectorGrid = L.GridLayer.extend({
4143 if ( this . options . getFeatureId ) {
4244 this . _vectorTiles = { } ;
4345 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 ) ;
5346 }
5447 this . _dataLayerNames = { } ;
55- } ,
48+ this . _userLayers = { } ;
49+ this . on ( 'tileunload' , function ( e ) {
50+ this . _tileUnload ( e ) ;
51+ } , this ) ;
52+ } ,
5653
5754 createTile : function ( coords , done ) {
5855 var storeFeatures = this . options . getFeatureId ;
56+ var onEachFeature = this . options . onEachFeature ;
5957
6058 var tileSize = this . getTileSize ( ) ;
6159 var renderer = this . options . rendererFactory ( coords , tileSize , this . options ) ;
@@ -103,11 +101,18 @@ L.VectorGrid = L.GridLayer.extend({
103101 }
104102
105103 if ( ! styleOptions . length ) {
104+ if ( onEachFeature ) {
105+ onEachFeature . call ( this , feat , null , layer , coords ) ;
106+ }
106107 continue ;
107108 }
108109
109110 var featureLayer = this . _createLayer ( feat , pxPerExtent ) ;
110111
112+ if ( onEachFeature ) {
113+ onEachFeature . call ( this , feat , null , layer , coords ) ;
114+ }
115+
111116 for ( var j = 0 ; j < styleOptions . length ; j ++ ) {
112117 var style = L . extend ( { } , L . Path . prototype . options , styleOptions [ j ] ) ;
113118 featureLayer . render ( renderer , style ) ;
@@ -186,6 +191,48 @@ L.VectorGrid = L.GridLayer.extend({
186191 return Object . keys ( this . _dataLayerNames ) ;
187192 } ,
188193
194+ vtGeometryToPoint : function ( geometry , vtLayer , tileCoords ) {
195+ var pxPerExtent = this . getTileSize ( ) . x / vtLayer . extent ;
196+ var tileSize = this . getTileSize ( ) ;
197+ var offset = tileCoords . scaleBy ( tileSize ) ;
198+ var point ;
199+ if ( typeof geometry [ 0 ] === 'object' && 'x' in geometry [ 0 ] ) {
200+ // Protobuf vector tiles return [{x: , y:}]
201+ point = L . point ( offset . x + ( geometry [ 0 ] . x * pxPerExtent ) , offset . y + ( geometry [ 0 ] . y * pxPerExtent ) ) ;
202+ } else {
203+ // Geojson-vt returns [,]
204+ point = L . point ( offset . x + ( geometry [ 0 ] * pxPerExtent ) , offset . y + ( geometry [ 1 ] * pxPerExtent ) ) ;
205+ }
206+ return point ;
207+ } ,
208+
209+ vtGeometryToLatLng : function ( geometry , vtLayer , tileCoords ) {
210+ return this . _map . unproject ( this . vtGeometryToPoint ( geometry , vtLayer , tileCoords ) ) ;
211+ } ,
212+
213+ addUserLayer : function ( userLayer , tileCoords ) {
214+ var tileKey = this . _tileCoordsToKey ( tileCoords ) ;
215+ this . _userLayers [ tileKey ] = this . _userLayers [ tileKey ] || [ ] ;
216+ this . _userLayers [ tileKey ] . push ( userLayer ) ;
217+ this . _map . addLayer ( userLayer ) ;
218+ } ,
219+
220+ _tileUnload : function ( e ) {
221+ var tileKey = this . _tileCoordsToKey ( e . coords ) ;
222+ if ( this . _vectorTiles ) {
223+ delete this . _vectorTiles [ tileKey ] ;
224+ }
225+ var userLayers = this . _userLayers [ tileKey ] ;
226+ if ( ! userLayers ) {
227+ return ;
228+ }
229+ for ( var i = 0 ; i < userLayers . length ; i ++ ) {
230+ console . log ( 'remove layer' ) ;
231+ this . _map . removeLayer ( userLayers [ i ] ) ;
232+ }
233+ delete this . _userLayers [ tileKey ] ;
234+ } ,
235+
189236 _updateStyles : function ( feat , renderer , styleOptions ) {
190237 styleOptions = ( styleOptions instanceof Function ) ?
191238 styleOptions ( feat . properties , renderer . getCoord ( ) . z ) :
0 commit comments