This is an update of my earlier post, and is an example of using ports to work with the ubiquitous Google Maps library.
Loading...
Elm side
All Elm code begins with the model, so let’s start there.
The model comprises the Google Maps object itself, which we simply store in Elm as a Json Value, and the center of the map.
If we are running full-screen, then when the program bootstraps the DOM has not been created, so we need to make the Google Maps Javascript wait for that before initiating the map. To do that, we force the Elm update loop to return immediately (with the current time, which we do not in practise use), and use the Tick message to trigger the port call to load the map.
The Tick content is dropped, but we now have the Dom available so we send the coordinates of the center of the map through the port loadMap to trigger our Javascript.
Notice the JSMap message. This has the Google Map attached to it, which we add to our model, and pass back to Javascript when we receive a North.
The map gets back to us through a port too, which we subscribe to when initialising the program:
Javascript side
We use elm.ports.<port name>.subscribe( payload => {...}) to receive messages from Elm, and elm.ports.<port name>.send(...) to pass data to Elm.