Hello,

I have printed a Drupal map using the following code:

    $map = \Drupal\openlayers\Openlayers::load('Map', 'mymap');
    $output = $map->build();
    drupal_add_js(array('mapid' => array('mapid' => $output['openlayers']['#attributes']['id'])), 'setting');
    return $output;
 

In the map, I used to access the map in javascript using the following code in openlayers 7.2.x:

    var map = $('#openlayers-map').data('openlayers');

Now, I believe it changed and I have to access it this way according to https://www.drupal.org/node/2600848

   var map = Drupal.openlayers.getMapById(Drupal.settings.mapid.mapid);

However, map returns false.

Am I doing anything wrong?

Comments

drupalpal created an issue. See original summary.

m.stenta’s picture

Version: 7.x-3.0-beta5 » 7.x-3.x-dev

Cleaning up the issue queue... moving this to 7.x-3.x...

Pol’s picture

Maybe m.stenta could help you with that.

m.stenta’s picture

Status: Active » Closed (fixed)

Yea you're in luck - I recently needed to do something similar! Here is how I got the map object in JS:

var id = $('.openlayers-map').attr('id');
var map = Drupal.openlayers.getMapById(id);

The first line looks up the ID of the map - this will only work if you have a single map on the page. But basically it's just trying to get the ID and there are other ways of doing that. The second line is what actually loads the map object.

Note the the "map" variable is the Drupal Openlayers module's map object, but it also contains the normal Openlayers library map object in:

map.map

Hope that helps!