Skip to content

Commit

Permalink
fix: add tiles-loaded event
Browse files Browse the repository at this point in the history
  • Loading branch information
vinayakkulkarni committed Sep 29, 2021
1 parent 72aa3b4 commit 3b99da0
Show file tree
Hide file tree
Showing 2 changed files with 50 additions and 17 deletions.
60 changes: 44 additions & 16 deletions components/map/CommonMap.vue
Original file line number Diff line number Diff line change
Expand Up @@ -432,6 +432,11 @@
required: true,
default: false,
},
tilesLoaded: {
type: Boolean as PropType<boolean>,
required: true,
default: false,
},
},
setup(props, { emit }: SetupContext) {
const { $config } = useContext();
Expand Down Expand Up @@ -501,24 +506,44 @@
function mapLoaded(e: { map: Map }): void {
map = e.map;
state.map.bbox = map.getBounds().toArray();
emit('update:loaded', true);
emit('update:style-changed', true);
emit('update:bounds', state.map.bbox);
/**
* This is required because we want
* to persist the layers on the map
* even after the basemap is changed
*/
map.on('style.load', () => {
const waiting = () => {
if (!map.isStyleLoaded()) {
emit('update:style-changed', false);
setTimeout(waiting, 200);
} else {
emit('update:style-changed', true);
const events: string[] = [
'style.load',
'sourcedata',
'sourcedataloading',
];
events.forEach((event) => {
// Sets map into loading state when tiles starts loading
map.on(event, () => {
// Sets map into loaded when tiles are loaded
if (event === 'sourcedata' || event === 'sourcedataloading') {
const waiting = () => {
if (!map.areTilesLoaded()) {
emit('update:tiles-loaded', false);
setTimeout(waiting, 200);
} else {
emit('update:tiles-loaded', true);
}
};
waiting();
}
if (event === 'style.load') {
/**
* This is required because we want
* to persist the layers on the map
* even after the basemap is changed
*/
const waiting = () => {
if (!map.isStyleLoaded()) {
emit('update:style-changed', false);
setTimeout(waiting, 200);
} else {
emit('update:style-changed', true);
}
};
waiting();
}
};
waiting();
});
});
/**
* Watch the pitchend event
Expand All @@ -532,6 +557,9 @@
10,
);
});
emit('update:loaded', true);
emit('update:style-changed', true);
emit('update:tiles-loaded', true);
}
/**
* This function emits a click event to the
Expand Down
7 changes: 6 additions & 1 deletion pages/index.vue
Original file line number Diff line number Diff line change
Expand Up @@ -39,6 +39,7 @@
:class="{ 'opacity-50': loading }"
:loaded.sync="state.map.loaded"
:style-changed.sync="state.map.styleChanged"
:tiles-loaded.sync="state.map.tilesLoaded"
:center.sync="state.map.center"
@click="mapClicked"
>
Expand Down Expand Up @@ -486,6 +487,7 @@
map: {
loaded: false as boolean,
styleChanged: false as boolean,
tilesLoaded: false as boolean,
center: [73.8567, 18.5204],
marker: {
center: [] as number[],
Expand All @@ -507,7 +509,10 @@
},
});
const loading = computed(
() => !state.map.loaded || !state.map.styleChanged,
() =>
!state.map.loaded ||
!state.map.styleChanged ||
!state.map.tilesLoaded,
);
onMounted(() => {
Expand Down

0 comments on commit 3b99da0

Please sign in to comment.