简体   繁体   English

如何使用OpenLayers显示Bing地图图层

[英]How to show Bing map layers with OpenLayers

I want to show the Bing map layers on the HTML page using OpenLayers. 我想使用OpenLayers在HTML页面上显示Bing地图图层。 I've got the Bing API too, but the map is not shown. 我也有Bing API,但未显示地图。 This is the code I have downloaded and changed. 这是我下载并更改的代码。 Is my API wrong? 我的API错误吗? I got the API recently from the site. 我最近从该站点获得了API。

<!DOCTYPE html>
<html>
  <head>
    <title>Show OSM map</title>
   <link rel="stylesheet" href="https://openlayers.org/en/v5.3.0/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>
    <style>
      html, body {
        height: 100%;
        width: 100%;
    padding: 0px;
    margin: 0px;
      } 
      .map {
        height: 90%;
        width: 100%;
      }
    </style>
    <script src="https://openlayers.org/en/v4.6.5/build/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map" "></div>
    <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>
       <option value="collinsBart">Collins Bart</option>
       <option value="ordnanceSurvey">Ordnance Survey</option>
     </select>
    </div>
    <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.layers.TileLayer({
          visible: false,
          preload: Infinity,
          source: new ol.source.BingMaps({
            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 map = new ol.Map({
        layers: layers,
        // Improve user experience by loading tiles while dragging/zooming. Will make
        // zooming choppy on mobile or slow devices.
        loadTilesWhileInteracting: true,
        target: 'map',
        view: new ol.View({
          center: [-6655.5402445057125, 6709968.258934638],
          zoom: 13
        })
      });

      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();
    </script>

  </body>
</html>

There are two syntax errors in that code 该代码中有两个语法错误

    layers.push(new ol.layers.TileLayer({

should be 应该

    layers.push(new ol.layer.Tile({

and

        imagerySet: styles[i]

needs a comma because it is followed by maxZoom 需要逗号,因为它后面是maxZoom

        imagerySet: styles[i],

Also Collins Bart os no longer supported, but there is a new Road dark style available 不再支持Collins Bart os,但有新的Road dark样式可用

<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>
   <option value="CanvasDark">Road dark</option>
   <option value="ordnanceSurvey">Ordnance Survey</option>
 </select>
</div>
<script>
 var styles = [
    'Road',
    'RoadOnDemand',
    'Aerial',
    'AerialWithLabels',
    'CanvasDark',
    'ordnanceSurvey'
  ];

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

相关问题 如何在openlayers地图上显示行车距离? - How to show driving distance on openlayers map? 传单:如何在地图上显示所有图层? - Leaflet: How to show all layers on a map? 来自两个不同来源的两个图层的 OpenLayers 地图缩放不同。 OpenLayers 可以改变缩放级别的行为吗? - OpenLayers map with two layers from two different sources zoom differently. Can OpenLayers change how zoom levels behave? 如何在openlayers中使用自己的bing地图样式 - How to use own styling of bing maps in openlayers 如何在OpenLayers的一个地图中使用带有lat-lon图层的Mapnik-Backgroundlayer? - How can I use a Mapnik-Backgroundlayer with lat-lon layers in one map with OpenLayers? OpenLayers:如何在地图div的流体CSS渲染后重新对齐鼠标坐标和矢量层 - OpenLayers: How to re-align mouse coordinates and vector layers after fluid css rendering of map div 如何嵌入必应地图 - How to embed a Bing Map 如何获得一个openlayers地图以显示在twitter引导程序中? - how to get an openlayers map to show inside twitter bootstrap? 如何使用OpenLayers创建一个单击的链接中心并在地图上显示一个弹出窗口? - How to make a clicked link center and show a popup on the map using OpenLayers? 如何从方位角等距重新投影图像雷达,以显示在 openlayers 地图上 - How to reprojection image radar from Azimuthal Equidistant, to show on openlayers map
 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM