function readOnly(count){ }
Starting November 20, the site will be set to read-only. On December 4, 2023,
forum discussions will move to the Trailblazer Community.
+ Start a Discussion
Rick BlakeRick Blake 

Build an Account Geolocation App: Using Events to Add Markers to the Map. map is null.

I found an error in a Trailhead, thought I'd help anyone else with the same problem. While trying to solve the Using Events to Add Markers to the Map section of the Build an Account Geolocation App trailhead I kept receiving an error about a null value. Debugging the code I found that the map variable in AccountMapController.js was null. This is because the accounts were loading faster than the Leaflet.js libraries. To get around this I changed the accountsLoaded function to recursively check if the map existed only add markers once the map was an object:

   if(component.get("v.map") != null){
            var map = component.get("v.map");

            alert(map);
            
            var accounts = event.getParam("accounts");
            for (var i=0; i<accounts.length; i++) {
                var account = accounts[i];
                var latLng = [account.Location__Latitude__s, account.Location__Longitude__s];
                L.marker(latLng, {account: account}).addTo(map);
            }  
        }else{
            accountsLoaded(component, event, null); 
        }