1

So, I'm trying to implement a custom map using the Google Maps API in addition to a Google Map view. I'm working with an up-to-date D7 install. The reason for this is that I need to have multiple layers of content types, which isn't possible with the default views user interface. The view needs to show multiple entity types as individual layers, which I hope to implement as GeoJSON data layers from GeoJSON views through a custom map implementation.

The problem is my current approach is breaking my existing GMAP views and is interfering with jquery functionality.

I get a series of 'jquery is undefined' errors, as well as broken UI functionality, leading me to believe something is making variables collide.

I am approaching this by creating a block in a custom module and adding the markup and JS necessary through the module like so.

add js files $path = drupal_get_path('module', 'gmap_json');  
drupal_add_js($path. '/gmap_json.js', 'file');

function gmap_json_block_info() {
    $blocks = array();  
    $blocks['hello_world'] = array(
      'info' => t('Hello world'),
    );  
    return $blocks;
  }  

  function gmap_json_block_view($delta = '') {
    $block = array();  
    if ($delta == 'hello_world') {
      $block['subject'] = t('Hello world');
      $block['content'] = t('This is the block content.');
      $block['content'] .= '<div id="json-map"></div>';
    }  
    return $block;
  }

And my map js is here

function initMap() {
    map = new google.maps.Map(document.getElementById('json-map'), {
      zoom: 4,
      center: {lat: -28, lng: 137}
    });


    map.data.loadGeoJson(
        'https://storage.googleapis.com/mapsdevsite/json/google.json');
  }

The problem seem to be with how I'm adding my scripts, since leaving the drupal_add_js statement out of the module is what triggers the error.

So, how am I messing up in adding the js to the page? What's the right way to do this?

| improve this question | | | | |
  • It would help to know what error you you are getting and what Drupal version you are using. – Neograph734 Feb 6 '19 at 17:56
  • 5
    You shouldn't have anything other than functions, constants and requires/includes in the global space; any API functions need to be called in the context of the request, not when your .module file is included - moving the drupal_add_js call into the hook_block_view implementation might fix it – Clive Feb 6 '19 at 19:26
  • Took me longer than it should have to completely understand, but this worked. thanks – nizz0k Feb 7 '19 at 9:55
  • @Clive your answer is correct here and solved the problem, so an answer post would be appropriate. I'm happy to flesh out on how to do this and post my own answer but I wouldn't want to steal your thunder. – nizz0k Feb 8 '19 at 9:09
0

So, in this case the answer comes from one of the comments. I wasn't properly using the drupal_add_js call, which shouldn't be called globally. So I moved it into the function in my little module to side step this, but the better way to approach this is using the render arrays and the #attached property of the block's content array. This approach let's you more granularly choose which js is placed and called.

| improve this answer | | | | |

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.