Reputation: 143
How can I go about making a drawn layer in Open Layers show on a map made from Bing map tiles? I have it working with the drawing layer showing but upon completion of drawing, the drawing goes away.
I believe my syntax must be incorrect as I can generate the simply version of open maps with drawing layers just fine, but when I combine with Bing, all falls apart.
<!DOCTYPE html>
<html>
<head>
<style>
html,
body {
margin: 0;
padding: 0;
width: 100%;
height: 100%;
}
#map {
width: 100%;
height: 800px;
}
textarea {
width: 300px;
height: 100px;
}
</style>
<title>Testing</title>
<link rel="stylesheet" href="https://openlayers.org/en/v4.6.4/css/ol.css" type="text/css">
<!-- The line below is only needed for old environments like Internet Explorer and Android 4.x -->
<script src="https://cdn.polyfill.io/v2/polyfill.min.js?features=requestAnimationFrame,Element.prototype.classList,URL"></script>
<script src="https://openlayers.org/en/v4.6.4/build/ol.js"></script>
</head>
<body>
<div id="map" class="map"></div>
<div id="demo"></div>
<select id="layer-select">
<option value="Aerial">Aerial</option>
<option value="AerialWithLabels" selected>Aerial with labels</option>
<option value="Road">Road (static)</option>
<option value="RoadOnDemand">Road (dynamic)</option>
</select>
<script>
var styles = [
'Road',
'RoadOnDemand',
'Aerial',
'AerialWithLabels',
'collinsBart',
'ordnanceSurvey'
];
var layers = [];
var i, ii;
for (i = 0, ii = styles.length; i < ii; ++i) {
layers.push(new ol.layer.Tile({
visible: false,
preload: Infinity,
source: new ol.source.BingMaps({
key: 'MY_BING_MAPS_KEY',
imagerySet: styles[i]
// use maxZoom 19 to see stretched tiles instead of the BingMaps
// "no photos at this zoom level" tiles
// maxZoom: 19
})
}));
}
var raster = new ol.layer.Tile({
source: new ol.source.OSM()
});
var source = new ol.source.Vector({
wrapX: false
});
var vector = new ol.layer.Vector({
source: source
});
var map = new ol.Map({
layers: layers,
loadTilesWhileInteracting: true,
target: 'map',
view: new ol.View({
center: ol.proj.transform([-95.5, 38.5], 'EPSG:4326', 'EPSG:3857'),
zoom: 5
})
});
var select = document.getElementById('layer-select');
function onChange() {
var style = select.value;
for (var i = 0, ii = layers.length; i < ii; ++i) {
layers[i].setVisible(styles[i] === style);
}
}
select.addEventListener('change', onChange);
onChange();
source.on('addfeature', function(evt) {
var feature = evt.feature;
var coords = feature.getGeometry().getCoordinates();
document.getElementById('demo').innerHTML = coords + "<br>";
});
var draw; // global so we can remove it later
function addInteraction() {
draw = new ol.interaction.Draw({
source: source,
type: 'Polygon',
freehand: true
});
layers.push(draw);
map.addInteraction(draw);
}
layers.push(draw);
addInteraction();
</script>
</body>
</html>
Upvotes: 0
Views: 560
Reputation: 51
map.addLayer()
is likely to solve this particular problem, if you pass the vector layer as a parameter.
Upvotes: 1
Reputation: 2022
You shall add the layer you are drawing to (vector) to the map.
var vector = new ol.layer.Vector({
source: source
});
layers.push(vector);
You can also remove these lines
layers.push(draw);
Upvotes: 0