How to properly attach Google Maps Api to Drupal behaviors in Drupal 7?
As you've figured out, your problem is one of closures. In the code that is not working, you have initMap() enclosed in the anonymous function (function($){}(jQuery)). This means it's not part of the global scope, and therefore cannot be called from the global scope.

The solution to this is to set your function to be available globally. But of course, you don't want namespace collisions, which is the whole point of wrapping code in an anonymous function in the first place.

There is a happy medium here. You create a global variable, and add your callback to that from within your anonymous function:

// Initialize your global object. Set the value to itself (if it exists)
// or an empty object otherwise.
var GoogleMaps = GoogleMaps || {};

// Start your anonymous function.
(function($, Drupal, GoogleMaps) {
  Drupal.behaviors.someKey = {
    attach:function () {
      GoogleMaps.initMap = function () {
        // Initialize your map here
      };
    }
  };
})(jQuery, Drupal, GoogleMaps);


Use &amp;callback=GoogleMaps.initMap in google maps api script embed.

You can also initialize your map from any other scripts using GoogleMaps.initMap()