1

i want to know how to name maps, put an id or something to find it later on openlayers, for example:

div id="map1">

div id="map2">

adition all code to set a map for fill it like:

map1 = new ol.Map({ id: 'Maps 1', target: 'map1' });

map2 = new ol.Map({ id: 'Maps 1', target: 'map2' });

then i could find the div for map 1 or 2 with

mapDiv1 = document.getElementbyId('map1');

mapDiv2 = document.getElementbyId('map2');

but how can i Access the map in mapDiv1 o mapDiv2 ?

also i would like how to do this:

$.getJSON(theme.url, null, function (data)

but with pure JavaScript, that is couse data only exist inside this function, even if i asign it to a global variable it gets me null, also i don't use jquery or other frameworks just JavaScript.

1

but how can I Access the map in mapDiv1 or mapDiv2 ?

Access can mean many things...

If by "access" you mean controlling the map: Normally you don't use the DOM elements for that. You control the map through your (openlayers) map-objects map1 and map2.

Examples:

var baselayer = new ol.layer.Tile({
    source: new ol.source.OSM({
        wrapX:false
    }),
    opacity: 0.7
});
var mpos = new ol.control.MousePosition({
    prefix: 'coordinates: '
});
map1.addLayer(baselayer);
map2.addLayer(baselayer);
map2.addControl(mpos);

If "access" ment you want to get the map as an image, then see this example: Export map.

Did you mean something else?

  • ok, the second question about $.getJSON i have solved with this code: > var xhttp; > xhttp = new XMLHttpRequest(); > xhttp.onreadystatechange = function () > { > if (xhttp.readyState == 4 && xhttp.status == 200) > { > document.getElementById('tb_ficha').innerHTML == xhttp.responseText; > return xhttp; > } > }; > xhttp.open("GET", url + feature.getId(), false); > xhttp.send(); – Arcangel Panda Jul 24 '16 at 21:17
  • 1
    about accesing i mean something like if i want to turn on/off visibility on some layers by his name, but i have 2 or more maps, and i don't have a global variable like map1 or map2, i could use some global array maybe to store object maps and find in there the reference, but maybe there could be other way to find a map objetc in mi dom controls ? – Arcangel Panda Jul 24 '16 at 21:21
  • maybe more explain: var baselayer = new ol.layer.Tile({ source: new ol.source.OSM({ wrapX:false }), opacity: 0.7 }); var mpos = new ol.control.MousePosition({ prefix: 'coordinates: ' }); map1.addLayer(baselayer); map2.addLayer(baselayer); map2.addControl(mpos); – Arcangel Panda Jul 24 '16 at 21:27
  • a control like: > <input type="checkbox" onchange="visible('layer1', this.checked)"> – Arcangel Panda Jul 24 '16 at 21:35
  • function: >function visible(themeId, estatus) >{ > var objeto = document.getElementById(themeId); > var theme; > _map.getLayers().forEach(function (lyr) > { > if (themeId == lyr.get('id')) > { > theme = lyr; > theme.setVisible(estatus); > if (estatus) > { > objeto.style.backgroundColor = "lightgrey"; > } > else > { > objeto.style.backgroundColor = "lightcoral"; > } > return; > } > }); >} – Arcangel Panda Jul 24 '16 at 21:35

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.