Updated to use spatialreference_changed instead of mapview_loaded to ensure the map was fully ready.