Thursday, 18 August 2016

javascript - why can't I load a layers list to select from to the map in openLayers 3, addEventListener and attachEvent are unavailable


I've tried to incorporate this sample that shows how to switch base layers into a page I am working on:


http://openlayers.org/en/v3.10.1/examples/bing-maps.html



When I try to load my page with the code included I get this error: Uncaught Error: addEventListener and attachEvent are unavailable. ol.js:34. I've googled this some but haven't found the answer yet. Here is all of the code for my page - I've pieced together code from various samples so I know it needs some cleanup, but wanted to include everything I have for reference. Can anyone help?


UPDATE:


I found that when I remove layerVector from the layers list to add to the map, I no longer get the error. Now my question is really, how can I have the functionality of choosing a base layer, while always maintaining the vector layer as well?





Vector layer example

















Answer



the problem comes from the way you declare layers when inisialising your map. so this piece of your code:


var map = new ol.Map({
layers: [baseLayers, layerVector],
loadTilesWhileInteracting: true,

target: document.getElementById('map'),
view: new ol.View({
//projection:projection
center: ol.proj.transform(
[-116, 42], 'EPSG:4326', 'EPSG:3857'),
zoom: 6
})
});

should change to this:



var layersToAdd = baseLayers.concat(layerVector);
var map = new ol.Map({
layers: layersToAdd ,
loadTilesWhileInteracting: true,
target: document.getElementById('map'),
view: new ol.View({
//projection:projection
center: ol.proj.transform(
[-116, 42], 'EPSG:4326', 'EPSG:3857'),
zoom: 6

})
});

It seems they way you pass layers on map is not a proper array. so concatenating the array of base layers with the vector layer, forms a proper array. I have also made o fiddle check it here to test. There are slight mods of your code but the piece I mentioned above seems to cause the problem. Hope to help you!!!!


No comments:

Post a Comment

arcpy - Changing output name when exporting data driven pages to JPG?

Is there a way to save the output JPG, changing the output file name to the page name, instead of page number? I mean changing the script fo...