将 OpenLayers 4 与 Angular 5 结合使用
Use OpenLayers 4 with Angular 5
我正在尝试在 Angular 5.
中使用 OpenLayers 4
基本上我只想实现来自官方 OpenLayers 站点的 QuickStart 示例。
到目前为止我做了什么:
npm install ol --save
下载ol包
- angular-cli.json 将这些行添加到 angular-cli.json。看到这必须在 Whosebug 上的另一个示例上完成。 ol.css 文件存在。 ol.js 文件没有。所以我不知道这是对还是错的方法,但显然行不通。
我的 angular 项目由 3 个部分组成:
-app
--console
--map
--sidebar
app.component.css
app.compinent.html
app.component.spec.ts
app.component.ts
app.module.ts
map.component.html
import { Component, OnInit } from '@angular/core';
import * as ol from '../../../node_modules/ol';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
mapId: string;
map: ol.Map = undefined;
constructor() { }
ngOnInit() {
this.map = new ol.Map({
target: this.mapId,
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
],
view: new ol.View({
center: ol.proj.fromLonLat([6.661594, 50.433237]),
zoom: 3,
})
});
}
}
谁能告诉我如何让它正常工作?
已更新为 Angular 8.x 和 OpenLayers 6.x:
安装 ol
并进行以下更改:
1.) 在html.index中添加CSS(确保CSS的版本与安装的版本相匹配ol
) :
<link rel="stylesheet" href="https://openlayers.org/en/v6.1.1/css/ol.css" type="text/css">
另一种方法是在angular.json:
中引用styles
对象中的CSS
"styles": [
"./node_modules/ol/ol.css",
"src/styles.scss"
],
2.) 在app.component.ts:
中创建地图
import { AfterViewInit, Component } from '@angular/core';
import { defaults as defaultControls } from 'ol/control';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import ZoomToExtent from 'ol/control/ZoomToExtent';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements AfterViewInit {
map: Map;
ngAfterViewInit() {
this.map = 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: [813079.7791264898, 5929220.284081122],
zoom: 7
}),
controls: defaultControls().extend([
new ZoomToExtent({
extent: [
813079.7791264898, 5929220.284081122,
848966.9639063801, 5936863.986909639
]
})
])
});
}
}
3.) app.component.css中的一些样式:
.map {
width: 100%;
height: 100vh;
}
4.) 最后是 app.component.html:
中的标记
<div id="map" class="map"></div>
也有空看看我的GitHub repo。
编辑:
如所述,将地图的目标设置在ngAfterViewInit
内可能会更好:
export class MapComponent implements OnInit, AfterViewInit {
...
ngAfterViewInit() {
this.map.setTarget('map');
}
}
ol
5.2 版的旧答案:
ol
是 OpenLayers 的正确包。但是,您不需要在 angular-cli.json.
中添加任何内容
随着最近的更新 ("ol": "^5.2.0"
),OpenLayers 的导入方式 类 和功能发生了一些变化。
map.component.ts:
import { Component, OnInit } from '@angular/core';
import OlMap from 'ol/Map';
import OlXYZ from 'ol/source/XYZ';
import OlTileLayer from 'ol/layer/Tile';
import OlView from 'ol/View';
import { fromLonLat } from 'ol/proj';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
map: OlMap;
source: OlXYZ;
layer: OlTileLayer;
view: OlView;
ngOnInit() {
this.source = new OlXYZ({
url: 'http://tile.osm.org/{z}/{x}/{y}.png'
});
this.layer = new OlTileLayer({
source: this.source
});
this.view = new OlView({
center: fromLonLat([6.661594, 50.433237]),
zoom: 3
});
this.map = new OlMap({
target: 'map',
layers: [this.layer],
view: this.view
});
}
}
map.component.html:
<div id="map" class="map"></div>
map.component.css:
.map {
width: 100%;
height: 100vh;
}
在 index.html:
的 header
-tag 中添加 OpenLayers 的 CSS
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/css/ol.css" type="text/css">
<style>html, body { margin: 0; }</style>
更早的答案:
您的组件可能如下所示:
import {Component, OnInit} from '@angular/core';
import OlMap from 'ol/map';
import OlXYZ from 'ol/source/xyz';
import OlTileLayer from 'ol/layer/tile';
import OlView from 'ol/view';
import OlProj from 'ol/proj';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
map: OlMap;
source: OlXYZ;
layer: OlTileLayer;
view: OlView;
constructor() {
}
ngOnInit() {
this.source = new OlXYZ({
url: 'http://tile.osm.org/{z}/{x}/{y}.png'
});
this.layer = new OlTileLayer({
source: this.source
});
this.view = new OlView({
center: OlProj.fromLonLat([6.661594, 50.433237]),
zoom: 3
});
this.map = new OlMap({
target: 'map',
layers: [this.layer],
view: this.view
});
}
}
CSS:
.map {
width: 100%;
height: 100vh;
}
HTML:
<div id="map" class="map"></div>
让我知道此解决方案是否适合您。
如果你不想从零开始用Angular5和OpenLayers4,有一个成熟的项目叫IGO2-lib which is based on Angular 5.x
, NodeJS
, OpenLayers 4.x
and Material Design
. IGO2也是一个完整的框架。
- 克隆存储库使用:
git clone https://github.com/infra-geo-ouverte/igo2-lib.git
- 在 cd igo2-lib/ 中部署并从以下位置安装:
npm install
- 开始表格:
npm start
- 在 http://localhost:4200/
打开浏览器
的现场演示
将此代码用于 ngAfterViewInit() 而不是 ngOnInit()
我正在尝试在 Angular 5.
中使用 OpenLayers 4基本上我只想实现来自官方 OpenLayers 站点的 QuickStart 示例。
到目前为止我做了什么:
npm install ol --save
下载ol包- angular-cli.json 将这些行添加到 angular-cli.json。看到这必须在 Whosebug 上的另一个示例上完成。 ol.css 文件存在。 ol.js 文件没有。所以我不知道这是对还是错的方法,但显然行不通。
我的 angular 项目由 3 个部分组成:
-app
--console
--map
--sidebar
app.component.css
app.compinent.html
app.component.spec.ts
app.component.ts
app.module.ts
map.component.html
import { Component, OnInit } from '@angular/core';
import * as ol from '../../../node_modules/ol';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
mapId: string;
map: ol.Map = undefined;
constructor() { }
ngOnInit() {
this.map = new ol.Map({
target: this.mapId,
layers: [
new ol.layer.Tile({
source: new ol.source.OSM(),
}),
],
view: new ol.View({
center: ol.proj.fromLonLat([6.661594, 50.433237]),
zoom: 3,
})
});
}
}
谁能告诉我如何让它正常工作?
已更新为 Angular 8.x 和 OpenLayers 6.x:
安装 ol
并进行以下更改:
1.) 在html.index中添加CSS(确保CSS的版本与安装的版本相匹配ol
) :
<link rel="stylesheet" href="https://openlayers.org/en/v6.1.1/css/ol.css" type="text/css">
另一种方法是在angular.json:
中引用styles
对象中的CSS
"styles": [
"./node_modules/ol/ol.css",
"src/styles.scss"
],
2.) 在app.component.ts:
中创建地图import { AfterViewInit, Component } from '@angular/core';
import { defaults as defaultControls } from 'ol/control';
import Map from 'ol/Map';
import View from 'ol/View';
import TileLayer from 'ol/layer/Tile';
import XYZ from 'ol/source/XYZ';
import ZoomToExtent from 'ol/control/ZoomToExtent';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.scss']
})
export class AppComponent implements AfterViewInit {
map: Map;
ngAfterViewInit() {
this.map = 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: [813079.7791264898, 5929220.284081122],
zoom: 7
}),
controls: defaultControls().extend([
new ZoomToExtent({
extent: [
813079.7791264898, 5929220.284081122,
848966.9639063801, 5936863.986909639
]
})
])
});
}
}
3.) app.component.css中的一些样式:
.map {
width: 100%;
height: 100vh;
}
4.) 最后是 app.component.html:
中的标记<div id="map" class="map"></div>
也有空看看我的GitHub repo。
编辑:
如ngAfterViewInit
内可能会更好:
export class MapComponent implements OnInit, AfterViewInit {
...
ngAfterViewInit() {
this.map.setTarget('map');
}
}
ol
5.2 版的旧答案:
ol
是 OpenLayers 的正确包。但是,您不需要在 angular-cli.json.
随着最近的更新 ("ol": "^5.2.0"
),OpenLayers 的导入方式 类 和功能发生了一些变化。
map.component.ts:
import { Component, OnInit } from '@angular/core';
import OlMap from 'ol/Map';
import OlXYZ from 'ol/source/XYZ';
import OlTileLayer from 'ol/layer/Tile';
import OlView from 'ol/View';
import { fromLonLat } from 'ol/proj';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
map: OlMap;
source: OlXYZ;
layer: OlTileLayer;
view: OlView;
ngOnInit() {
this.source = new OlXYZ({
url: 'http://tile.osm.org/{z}/{x}/{y}.png'
});
this.layer = new OlTileLayer({
source: this.source
});
this.view = new OlView({
center: fromLonLat([6.661594, 50.433237]),
zoom: 3
});
this.map = new OlMap({
target: 'map',
layers: [this.layer],
view: this.view
});
}
}
map.component.html:
<div id="map" class="map"></div>
map.component.css:
.map {
width: 100%;
height: 100vh;
}
在 index.html:
的header
-tag 中添加 OpenLayers 的 CSS
<link rel="stylesheet" href="https://cdn.rawgit.com/openlayers/openlayers.github.io/master/en/v5.1.3/css/ol.css" type="text/css">
<style>html, body { margin: 0; }</style>
更早的答案:
您的组件可能如下所示:
import {Component, OnInit} from '@angular/core';
import OlMap from 'ol/map';
import OlXYZ from 'ol/source/xyz';
import OlTileLayer from 'ol/layer/tile';
import OlView from 'ol/view';
import OlProj from 'ol/proj';
@Component({
selector: 'app-map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {
map: OlMap;
source: OlXYZ;
layer: OlTileLayer;
view: OlView;
constructor() {
}
ngOnInit() {
this.source = new OlXYZ({
url: 'http://tile.osm.org/{z}/{x}/{y}.png'
});
this.layer = new OlTileLayer({
source: this.source
});
this.view = new OlView({
center: OlProj.fromLonLat([6.661594, 50.433237]),
zoom: 3
});
this.map = new OlMap({
target: 'map',
layers: [this.layer],
view: this.view
});
}
}
CSS:
.map {
width: 100%;
height: 100vh;
}
HTML:
<div id="map" class="map"></div>
让我知道此解决方案是否适合您。
如果你不想从零开始用Angular5和OpenLayers4,有一个成熟的项目叫IGO2-lib which is based on Angular 5.x
, NodeJS
, OpenLayers 4.x
and Material Design
. IGO2也是一个完整的框架。
- 克隆存储库使用:
git clone https://github.com/infra-geo-ouverte/igo2-lib.git
- 在 cd igo2-lib/ 中部署并从以下位置安装:
npm install
- 开始表格:
npm start
- 在 http://localhost:4200/ 打开浏览器
将此代码用于 ngAfterViewInit() 而不是 ngOnInit()