Giu*_*ppe 13 openlayers angular angular5
我正在尝试在Angular 5中使用OpenLayers 4.
基本上我只是想从官方的OpenLayers网站实现QuickStart示例.
到目前为止我做了什么:
npm install ol --save
下载ol包我的角度项目包括3个组件:
-app
--console
--map
--sidebar
app.component.css
app.compinent.html
app.component.spec.ts
app.component.ts
app.module.ts
Run Code Online (Sandbox Code Playgroud)
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,
})
});
}
}
Run Code Online (Sandbox Code Playgroud)
任何人都可以告诉如何正常工作?
pza*_*ger 29
ol
是OpenLayers的正确包.但是,您不需要在angular-cli.json中添加任何内容.
编辑5.2版本ngAfterViewInit
:
使用最近的update(ol
),导入OpenLayers的类和函数的方式有所改变.
map.component.ts:
<link rel="stylesheet" href="https://openlayers.org/en/v6.1.1/css/ol.css" type="text/css">
Run Code Online (Sandbox Code Playgroud)
map.component.html:
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
]
})
])
});
}
}
Run Code Online (Sandbox Code Playgroud)
map.component.css:
.map {
width: 100%;
height: 100vh;
}
Run Code Online (Sandbox Code Playgroud)
在index.html的ol
-tag中添加OpenLayers的CSS :
<div id="map" class="map"></div>
Run Code Online (Sandbox Code Playgroud)
老答案:
您的组件可能如下所示:
export class MapComponent implements OnInit, AfterViewInit {
...
ngAfterViewInit() {
this.map.setTarget('map');
}
}
Run Code Online (Sandbox Code Playgroud)
CSS:
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
});
}
}
Run Code Online (Sandbox Code Playgroud)
HTML:
<div id="map" class="map"></div>
Run Code Online (Sandbox Code Playgroud)
如果此解决方案适合您,请告诉我.
小智 8
如果你不想从零与Angular5和OpenLayers4开始,有一个叫做成熟的项目IGO2-LIB这是基于Angular 5.x
,NodeJS
,OpenLayers 4.x
和Material Design
.IGO2也是一个完整的框架.
git clone https://github.com/infra-geo-ouverte/igo2-lib.git
npm install
npm start
这是IGO2的现场演示:https://geoegl.msp.gouv.qc.ca/igo2/apercu-qc/
归档时间: |
|
查看次数: |
24086 次 |
最近记录: |