openlayers-3openlayers-3入门


备注

本节概述了openlayers-3是什么,以及开发人员可能想要使用它的原因。

它还应该提到openlayers-3中的任何大型主题,并链接到相关主题。由于openlayers-3的文档是新的,您可能需要创建这些相关主题的初始版本。

使用Bing地图的示例

var baseLayer = new ol.layer.Tile({
    visible: true,
    preload: Infinity,
    source: new ol.source.BingMaps({
        // We need a key to get the layer from the provider. 
        // Sign in with Bing Maps and you will get your key (for free)
        key: 'Ap9VqFbJYRNkatdxt3KyzfJxXN_9GlfABRyX3k_JsQTkMQLfK_-AzDyJHI5nojyP',
        imagerySet: 'Aerial', // or 'Road', 'AerialWithLabels', etc.
        // use maxZoom 19 to see stretched tiles instead of the Bing Maps
        // "no photos at this zoom level" tiles
        maxZoom: 19
    })
});

var map = new ol.Map({ 
    layers: [baseLayer],
    target: 'map', 
    controls: ol.control.defaults({ 
        attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
        })
    }),
    view: new ol.View({ 
        center: ol.proj.fromLonLat([0, 0]),
        zoom: 2 
    })
});
 

开始使用简单的地图

<html>
  <head>
    <title>Getting started</title>
    <link rel="stylesheet" href="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.17.1/ol.css" type="text/css">
    <script src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.17.1/ol.js"></script>
  </head>
  <body>
    <div id="map" class="map"></div>
    <script>
      var baseLayer= new ol.layer.Tile({ //a Tile layer is a the background layer for the map
        // here we choose an OpenStreetMap base layer
        source: new ol.source.OSM({
          url: 'https://a.tile.openstreetmap.org/{z}/{x}/{y}.png'
        }) 
      });

      var map = new ol.Map({ // we create our map
        layers: [baseLayer], // and add the layers to it ( in our case we only have one)
        target: 'map', // the div element that will serve as a map
        controls: ol.control.defaults({ // we leave the map controls to default
          attributionOptions: /** @type {olx.control.AttributionOptions} */ ({
            collapsible: false
          })
        }),
        view: new ol.View({ // we define the initial view of the map
          center: ol.proj.fromLonLat([0, 0]), //the default projection is the spherical mercator (meter units) so we get coordinates of the center by degrees
          zoom: 2 // the initial zoom level
        })
      });
    </script>
  </body>
</html>
 

安装或设置

OpenLayers 3或者它被称为OL-3是一个用于Web映射的Javascript库,因此为了使用它,您需要将它添加到您的html中:

  • 首先添加ol.css文件以使用OL-3的地图样式:

  • 然后添加ol.js文件:

您也可以从官方网站www.openlayers.org下载OL-3,并通过更改src和href来调用html中的文件

设置OL-3

<link rel="stylesheet" href="http://openlayers.org/en/v3.17.1/css/ol.css" type="text/css">

<script src="http://openlayers.org/en/v3.17.1/build/ol.js"></script>