天天看點

OpenLayers初體驗0x1:使用Node建立OpenLayers應用項目初始化JS和HTML 建立一個bundle

項目初始化

mkdir new-project
cd new-project

npm init

npm install ol
npm install --save-dev parcel-bundler
           
OpenLayers初體驗0x1:使用Node建立OpenLayers應用項目初始化JS和HTML 建立一個bundle

JS和HTML

//index.js
import 'ol/ol.css';
import {Map ,View} from 'ol';
import TileLayer from 'ol/layer/Tile';
import OSM from 'ol/source/OSM';

const map = new Map({
    target:'map',
    layers:[
        new TileLayer({
            source:new OSM()
        })
    ],
    view:new View({
        center:[0,0],
        zoom:0
    })
});
           
<!--index.html-->
<!DOCTYPE html>
<html>
    <head>
        <meta charset="utf-8">
        <title>Using Parcel with OpenLayers</title>
        <style>
            #map{
                width: 400px;
                height: 250px;
            }
        </style>
    </head>
    <body>
        <div id="map"></div>
        <script src="./index.js"></script>
    </body>
</html>
           

 建立一個bundle

"scripts": {
    "test": "echo \"Error: no test specified\" && exit 1",
    "start":"parcel index.html",
    "build":"parcel build --public-url . index.html"
  }
           
OpenLayers初體驗0x1:使用Node建立OpenLayers應用項目初始化JS和HTML 建立一個bundle

繼續閱讀