【发布时间】:2021-03-19 06:50:31
【问题描述】:
我正在研究此链接中提到的 OpenLayers 研讨会:
https://openlayers.org/workshop/en/vector/geojson.html
代码在运行时应根据 URL 中提到的 geojson 文件将地图显示为矢量数据,如 代码如下。
我现在遇到的问题是,当我运行代码时,没有地图出现只有加号和减号按钮,背景是深色的 但矢量数据不升值。
为了解决这个问题,我尝试将文件的格式从 geojson 更改为 json 并尝试操作 文件路径如下:
尝试:
我还将代码修改为如下:
url: './data/countries.geojson',
url: '/data/countries.geojson',
url: 'data/countries.geojson',
但它们都不起作用,也没有显示地图。 请告诉我如何将地图呈现或显示为来自 URL 的矢量数据
main.js:
import 'ol/ol.css';
import GeoJSON from 'ol/format/GeoJSON';
import Map from 'ol/Map';
import VectorLayer from 'ol/layer/Vector';
import VectorSource from 'ol/source/Vector';
import View from 'ol/View';
import {Fill, Stroke, Style, Text} from 'ol/style';
var style = new Style({
fill: new Fill({
color: 'rgba(255, 255, 255, 0.6)',
}),
stroke: new Stroke({
color: '#319FD3',
width: 1,
}),
text: new Text({
font: '12px Calibri,sans-serif',
fill: new Fill({
color: '#000',
}),
stroke: new Stroke({
color: '#fff',
width: 3,
}),
}),
});
var vectorLayer = new VectorLayer({
source: new VectorSource({
url: 'data/countries.geojson',
format: new GeoJSON(),
}),
style: function (feature) {
style.getText().setText(feature.get('name'));
return style;
},
});
var map = new Map({
layers: [vectorLayer],
target: 'map-container',
view: new View({
center: [0, 0],
zoom: 1,
}),
});
index.html:
<!DOCTYPE html>
<html>
<head>
<meta charset="utf-8">
<title>OpenLayers</title>
<style>
html, body, #map-container {
margin: 0;
height: 100%;
width: 100%;
font-family: sans-serif;
background-color: #04041b;
}
</style>
</head>
<body>
<div id="map-container"></div>
<script src="main.js"></script>
</body>
</html>
【问题讨论】:
标签: javascript html openlayers openlayers-3