如何在由Bing地图图块制作的地图上显示“打开图层”中的绘制图层?我让它与绘图层一起工作,但在完成绘图后,绘图就消失了。
我相信我的语法必须不正确,因为我可以生成简单版本的开放地图,绘图层很好,但是当我与Bing结合时,一切都崩溃了。
<!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>
答案 0 :(得分:1)
map.addLayer()
可能会解决此特定问题。
答案 1 :(得分:0)
您应将要绘制的图层(矢量)添加到地图中。
var vector = new ol.layer.Vector({
source: source
});
layers.push(vector);
您也可以删除这些行
layers.push(draw);