0

This is a follow on to this question if you are wondering why I'd want to do this.

I need to create a mapkit JS map, then destroy it when its done being created. I load the mapkit API. Then after that I create the map:

mapContainerRef.current=document?.createElement('div');
mapContainerRef.current.style.width='0';
mapContainerRef.current.style.height='0';
mapContainerRef.current.style.position='absolute';
mapContainerRef.current.style.opacity='0';
document.body.appendChild(mapContainerRef.current);

// This line starts loading more scripts, when are they done loading?
const newMap=new mapkit.Map(mapContainerRef.current);

setMap(newMap);

After this happens, the map makes a few API calls. I tested this. I loaded the mapkit script, then waited 5 seconds, then created a new map. Here are the calls that were made when the new map was created.

enter image description here

I need to figure out when it's done doing all the work it is doing so I can destroy it and unmount the map.

There is no event handler to say it's ready to go, there is an undocumented property: map._impl.state that changes from Initialized to Ready, but this happens right away and before all the work is done, so that is of no help.

If you destroy a map right after creating it, it produces an error:

Uncaught TypeError: Cannot set property 'language' of null
    at n._reConfigureMapNode (VM28093 mapkit.js:3)
    at n._enter_syrup_init (VM28093 mapkit.js:3)
    at n._updateState (VM28093 mapkit.js:3)
    at n._transition (VM28093 mapkit.js:3)
    at n.handleSpileLoad (VM28093 mapkit.js:3)
    at n.<anonymous> (VM28093 mapkit.js:3)
    at n.<anonymous> (VM28093 mapkit.js:3)

Currently I'm just setting a timeout and hoping it's enough....

Diesel
  • 5,099
  • 7
  • 43
  • 81
  • https://developer.apple.com/documentation/mapkitjs/mapkit/handling_initialization_events – Adam Jenkins Jul 19 '20 at 21:19
  • 1
    That's for `mapkit`, not a `map`. All the `mapkit` initialization is already done and they have good methods for that. As you can see from the first lines of code, `mapkit` is being used to create a `map` and is already loaded. Clarified my question. – Diesel Jul 19 '20 at 21:24

0 Answers0