Logo Questions Linux Laravel Mysql Ubuntu Git Menu
 

Openlayers3 get map from element

Is there a way in Openlayers 3 to get the map that is attached to a specific html element?

Something like:

var map = new ol.Map({
  view: new ol.View({
    center: [0, 0],
    zoom: 1
  }),
  layers: [
    new ol.layer.Tile({
      source: new ol.source.MapQuest({layer: 'osm'})
    })
  ],
  target: 'map'
});

//Later on, in a different file
var myMap = $("#map").ol.Map()
like image 539
Tyler DeWitt Avatar asked Apr 28 '15 14:04

Tyler DeWitt


1 Answers

The map object has a reference to the HTML element but the HTML element does not have a reference to the map object. The HTML element does not know about the map object at all.

If you use jQuery you could possibly store a reference to the map in the jQuery object using the data method. For example:

var map = new ol.Map({
  target: 'map', 
  //... 
});
$('#map').data('map', map);

And then, to get a reference to the map from the element:

var map = $('#map').data('map');
like image 105
erilem Avatar answered Sep 28 '22 04:09

erilem