Error is caused by ‘Uncaught ReferenceError: jQuery is not defined’.
Inline script in function generateMapJS is loaded before jQuery.
Found a solution from https://stackoverflow.com/questions/41394983/how-to-defer-inline-javascript.
<script>
window.addEventListener('DOMContentLoaded', function() {
(function($) {
//script code
})(jQuery);
});
</script>