美文网首页
ol-webpack

ol-webpack

作者: hehehehe | 来源:发表于2020-10-29 12:06 被阅读0次

    npm install
    npm run build

    webpack.config.js

    const webpack = require('webpack');
    
    module.exports = {
      entry: './main.js',
      output: {
        path: __dirname,
        filename: 'bundle.js'
      },
    };
    

    package.json

    {
      "name": "ol-webpack",
      "version": "1.0.0",
      "description": "Example using OpenLayers with Webpack",
      "scripts": {
        "build": "webpack --config webpack.config.js --mode production"
      },
      "devDependencies": {
        "webpack": "^4.12.1",
        "webpack-command": "^0.3.0"
      },
      "dependencies": {
        "ol": "^5.1.2"
      }
    }
    

    main.js

    import {Map, View} from 'ol';
    import TileLayer from 'ol/layer/Tile';
    import XYZ from 'ol/source/XYZ';
    
    new Map({
      target: 'map',
      layers: [
        new TileLayer({
          source: new XYZ({
            url: 'https://{a-c}.tile.openstreetmap.org/{z}/{x}/{y}.png'
          })
        })
      ],
      view: new View({
        center: [0, 0],
        zoom: 2
      })
    });
    

    index.html

    <!DOCTYPE html>
    <html>
      <head>
        <meta charset="utf-8">
        <title>Using OpenLayers with Webpack</title>
        <link rel="stylesheet" href="https://openlayers.org/en/latest/css/ol.css" type="text/css">
        <style>
          html, body {
            margin: 0;
            height: 100%;
          }
          #map {
            position: absolute;
            top: 0;
            bottom: 0;
            width: 100%;
          }
        </style>
      </head>
      <body>
        <div id="map"></div>
        <script src="./bundle.js"></script>
      </body>
    </html>
    
    

    相关文章

      网友评论

          本文标题:ol-webpack

          本文链接:https://www.haomeiwen.com/subject/zxykvktx.html