javascript - 在 Node.js View 中创建 OpenLayers map

标签 javascript node.js express pug openlayers-3

我是 node.js 的新手,正在尝试在页面上放置一个简单的 OpenLayers map 。但是, map 没有显示。

代码:

app.js

var express = require('express'); 
var app = express(); 

app.set('view engine', 'jade');
app.use(express.static(__dirname + '/public'));


app.get('/', function(req, res){
    res.render('index', {title: 'Map Viewer'}); 
}); 

app.get('/map', function(req, res){
    res.render('view'); 
}); 

app.listen(3000, function(){
    console.log('Server listening on port 3000...'); 
}); 

layout.jade

doctype html
html
    head
    title= title
    link(rel='stylesheet', href='/stylesheets/style.css')
  
    body
        block content

view.jade

extends layout 
block content 
    #map 


script(type='text/javascript').

var map = new ol.Map({
    target: 'map',
    layers: [
      new ol.layer.Tile({
        title: 'Global Imagery',
        source: new ol.source.TileWMS({
          url: 'http://demo.opengeo.org/geoserver/wms',
          params: {LAYERS: 'nasa:bluemarble', VERSION: '1.1.1'}
        })
      })
    ],
    view: new ol.View({
      projection: 'EPSG:4326',
      center: [0, 0],
      zoom: 0,
      maxResolution: 0.703125
    })
  }); 

package.json 中,我正在使用 "openlayers": "^3.15.1"

结果:

上面的代码生成了一个包含以下 html 的空白页面:

<html>
    <head>
    </head>
    <title>
    </title>
    <link rel="stylesheet" href="/stylesheets/style.css"> 
    <body>
        <div id="map"> 
        </div>
    </body>
</html>

知道我做错了什么吗?

最佳答案

第一个问题是 view.jade 中的 Javascript block 没有缩进。 script(type='text/javascript'). 下的所有内容(包括在内)都需要缩进一个空格。

另一个问题是 ol.js 没有被导入。在 layout.jade 中必须添加以下行:

script(type='text/javascript' src="https://cdnjs.cloudflare.com/ajax/libs/ol3/3.15.1/ol.js")

关于javascript - 在 Node.js View 中创建 OpenLayers map ,我们在Stack Overflow上找到一个类似的问题: https://stackoverflow.com/questions/36632467/

相关文章:

javascript - HighChart饼图不显示任何数据

node.js - 如何使用 Dropbox Lepton Node JS 包压缩图像

javascript - 使用express.js 打破程序流程

javascript - 从 Await 调用中捕获错误后,如何停止在 Express.js 中执行?

Javascript 变量不作用于全局

javascript - jquery 自动完成功能不起作用

javascript - Instagram API,/media/recent 只显示当年的图像?

mysql - 如何使用 MySQL 进行 SQL-Fullstack-Generator?

node.js - 为什么 Debian/Ubuntu 上只能使用过时的 NPM 版本?

node.js - 如何通过 Express 路由器内的套接字发出消息