Skip to content

Commit 2e8a315

Browse files
committed
Get rendering of map-tile kinda working.
1 parent 0170134 commit 2e8a315

File tree

3 files changed

+89
-13
lines changed

3 files changed

+89
-13
lines changed

src/map-tile.js

Lines changed: 4 additions & 3 deletions
Original file line numberDiff line numberDiff line change
@@ -166,11 +166,12 @@ export class HTMLTileElement extends HTMLElement {
166166
// Create a new MapTileLayer
167167
this._tileLayer = mapTileLayer({
168168
projection: this.getMapEl()._map.options.projection,
169-
mapTiles: [this],
170-
opacity: 1
169+
opacity: 1,
170+
pane: parentElement._templatedLayer.getContainer()
171171
});
172+
this._tileLayer.addMapTile(this);
172173

173-
// If there's a FeaturesTilesLayerGroup in the parentElement, add the layer to it
174+
// add MapTileLayer to TemplatedFeaturesOrTilesLayerGroup of the parentElement
174175
if (
175176
parentElement._templatedLayer &&
176177
parentElement._templatedLayer.addLayer

src/mapml/layers/MapTileLayer.js

Lines changed: 78 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -27,13 +27,26 @@ export var MapTileLayer = GridLayer.extend({
2727
GridLayer.prototype.initialize.call(this, options);
2828
this._mapTiles = options.mapTiles || [];
2929
this._tileMap = {};
30+
this._pendingTiles = {};
3031
this._buildTileMap();
31-
32+
this._container = DomUtil.create('div', 'leaflet-layer');
33+
DomUtil.addClass(this._container, 'mapml-static-tile-container');
3234
// Store bounds for visibility checks
3335
// this.layerBounds = this._computeLayerBounds();
3436
// this.zoomBounds = this._computeZoomBounds();
3537
},
3638

39+
onAdd: function (map) {
40+
this.options.pane.appendChild(this._container);
41+
// Call the parent method
42+
GridLayer.prototype.onAdd.call(this, map);
43+
},
44+
onRemove: function (map) {
45+
// Clean up pending tiles
46+
this._pendingTiles = {};
47+
DomUtil.remove(this._container);
48+
},
49+
3750
/**
3851
* Adds a map-tile element to the layer
3952
* @param {HTMLTileElement} mapTile - The map-tile element to add
@@ -91,14 +104,18 @@ export var MapTileLayer = GridLayer.extend({
91104
/**
92105
* Overrides GridLayer createTile to use map-tile elements
93106
* @param {Object} coords - Tile coordinates
107+
* @param {Function} done - Callback to be called when the tile is ready, with error and tile element params
94108
* @returns {HTMLElement} - The created tile element
95109
*/
96-
createTile: function (coords) {
110+
createTile: function (coords, done) {
97111
const tileKey = this._tileCoordsToKey(coords);
98112
const tileSize = this.getTileSize();
99113

100114
// Create container element
101115
const tileElement = document.createElement('div');
116+
tileElement.setAttribute('col', coords.x);
117+
tileElement.setAttribute('row', coords.y);
118+
tileElement.setAttribute('zoom', coords.z);
102119
DomUtil.addClass(tileElement, 'leaflet-tile');
103120

104121
// Set size
@@ -116,11 +133,30 @@ export var MapTileLayer = GridLayer.extend({
116133
img.height = tileSize.y;
117134
img.alt = '';
118135
img.setAttribute('role', 'presentation');
136+
// bidirectional link map-tile element and rendered div
137+
tileElement._mapTile = matchingTile;
138+
matchingTile._tileDiv = tileElement;
119139

120140
tileElement.appendChild(img);
121141

122-
// This is needed for Leaflet to properly handle the tile
123-
this._tileReady(coords, null, tileElement);
142+
// Add the loaded class manually to ensure tile is visible
143+
DomUtil.addClass(tileElement, 'leaflet-tile-loaded');
144+
145+
// Call the done callback to signal that the tile is ready
146+
done(null, tileElement);
147+
} else {
148+
// The tile might be added later, register a pending tile
149+
if (!this._pendingTiles) {
150+
this._pendingTiles = {};
151+
}
152+
153+
// Store the tile element and done callback for later update
154+
this._pendingTiles[tileKey] = {
155+
element: tileElement,
156+
done: done
157+
};
158+
159+
// Don't call done yet - we'll call it when the map-tile is added
124160
}
125161

126162
return tileElement;
@@ -145,8 +181,40 @@ export var MapTileLayer = GridLayer.extend({
145181
_addToTileMap: function (mapTile) {
146182
const tileKey = `${mapTile.col}:${mapTile.row}:${mapTile.zoom}`;
147183
this._tileMap[tileKey] = mapTile;
148-
},
149184

185+
// Check if this tile was requested but not available at that time
186+
if (this._pendingTiles && this._pendingTiles[tileKey]) {
187+
const pendingTile = this._pendingTiles[tileKey];
188+
const tileElement = pendingTile.element;
189+
const doneCallback = pendingTile.done;
190+
191+
// Create and append the image to the tile
192+
const tileSize = this.getTileSize();
193+
const img = document.createElement('img');
194+
img.src = mapTile.src;
195+
img.width = tileSize.x;
196+
img.height = tileSize.y;
197+
img.alt = '';
198+
img.setAttribute('role', 'presentation');
199+
200+
// bidirectional link map-tile element and rendered div
201+
tileElement._mapTile = mapTile;
202+
mapTile._tileDiv = tileElement;
203+
204+
tileElement.appendChild(img);
205+
206+
// Add the loaded class manually to ensure tile is visible
207+
DomUtil.addClass(tileElement, 'leaflet-tile-loaded');
208+
209+
// Call the done callback to signal that the tile is now ready
210+
if (doneCallback) {
211+
doneCallback(null, tileElement);
212+
}
213+
214+
// Remove from pending tiles
215+
delete this._pendingTiles[tileKey];
216+
}
217+
},
150218
/**
151219
* Removes a map-tile element from the tile map
152220
* @param {HTMLTileElement} mapTile - The map-tile element to remove
@@ -155,6 +223,11 @@ export var MapTileLayer = GridLayer.extend({
155223
_removeFromTileMap: function (mapTile) {
156224
const tileKey = `${mapTile.col}:${mapTile.row}:${mapTile.zoom}`;
157225
delete this._tileMap[tileKey];
226+
227+
// Also remove from pending tiles if it exists there
228+
if (this._pendingTiles && this._pendingTiles[tileKey]) {
229+
delete this._pendingTiles[tileKey];
230+
}
158231
},
159232

160233
/**

src/mapml/layers/TemplatedFeaturesOrTilesLayerGroup.js

Lines changed: 7 additions & 5 deletions
Original file line numberDiff line numberDiff line change
@@ -68,14 +68,16 @@ export var TemplatedFeaturesOrTilesLayerGroup = LayerGroup.extend({
6868
onRemove: function (map) {
6969
// Remove container from DOM, but not from this
7070
DomUtil.remove(this._container);
71+
// should clean up the container
72+
for (let child of this._container.children) {
73+
DomUtil.remove(child);
74+
}
7175

7276
// Remove each layer from the map, but does not clearLayers
7377
LayerGroup.prototype.onRemove.call(this, map);
74-
75-
this.clearLayers();
76-
77-
// Clear reference to the map
78-
this._map = null;
78+
},
79+
getContainer: function () {
80+
return this._container;
7981
},
8082
getEvents: function () {
8183
var events = {

0 commit comments

Comments
 (0)