在 typescript/angular2 中使用 openlayer3
Using openlayer3 in typescript/angular2
我有一个使用 openLayer3 的 Javascript 代码。我需要在 angular2 项目中使用 Typescript 实现此代码。
有人知道如何将 openlayer 与 angular2 / Typescript 一起使用吗?
非常感谢,
约翰
至于你可能对 DefinitelyTyped 项目感兴趣的打字 - 你可以在那里找到 openlayers/openlayers.d.ts - 你可能需要了解 tsd
, 文件夹会议等
至于 AngularJS 2,考虑到它仍处于测试阶段,经验告诉您主要靠自己。谷歌搜索仍然可以指向您,即 https://gist.github.com/borchsenius/5a1ec0b48b283ba65021.
通常的做法是先根据已有的例子来理解AngularJS 2 方式。您应该很快就会注意到不同集成的外观有很多常识。然后尝试以适当的方式调整您想要的内容。然后是进一步提供和分享知识的重要部分:)
同时了解现有的 AngularJS 1.x 解决方案,例如 this article 可能会有所帮助。
1.选项 A(使用 Angular CLI)
在你的 .angular-cli.json 添加 Openlayers3(位于你的项目根目录):
...
"styles": [
"../node_modules/openlayers/dist/ol.css"
],
"scripts": [
"../node_modules/openlayers/dist/ol.js"
],
...
1.选项 B(不适用于 Angular CLI)
在您的 index.html 中添加 Openlayers3("usual" 方式):
<script src="node_modules/openlayers/dist/ol.js"></script>
<link rel="stylesheet" href="node_modules/openlayers/dist/ol.css">
2。从您的打字稿文件访问 ol:
import { AfterViewInit, Component, ElementRef, ViewChild } from "@angular/core";
// This is necessary to access ol3!
declare var ol: any;
@Component({
selector: 'my-app',
template: `
<h3> The map </h3>
<div #mapElement id="map" class="map"> </div>
`
// The "#" (template reference variable) matters to access the map element with the ViewChild decorator!
})
export class AppComponent implements AfterViewInit {
// This is necessary to access the html element to set the map target (after view init)!
@ViewChild("mapElement") mapElement: ElementRef;
public map: any;
constructor(){
var osm_layer: any = new ol.layer.Tile({
source: new ol.source.OSM()
});
// note that the target cannot be set here!
this.map = new ol.Map({
layers: [osm_layer],
view: new ol.View({
center: ol.proj.transform([0,0], 'EPSG:4326', 'EPSG:3857'),
zoom: 2
})
});
}
// After view init the map target can be set!
ngAfterViewInit() {
this.map.setTarget(this.mapElement.nativeElement.id);
}
}
您可以使用 angular2-openlayers,可作为 npm 包或在此处获得:https://github.com/quentin-ol/angular2-openlayers
我有一个使用 openLayer3 的 Javascript 代码。我需要在 angular2 项目中使用 Typescript 实现此代码。
有人知道如何将 openlayer 与 angular2 / Typescript 一起使用吗?
非常感谢,
约翰
至于你可能对 DefinitelyTyped 项目感兴趣的打字 - 你可以在那里找到 openlayers/openlayers.d.ts - 你可能需要了解 tsd
, 文件夹会议等
至于 AngularJS 2,考虑到它仍处于测试阶段,经验告诉您主要靠自己。谷歌搜索仍然可以指向您,即 https://gist.github.com/borchsenius/5a1ec0b48b283ba65021.
通常的做法是先根据已有的例子来理解AngularJS 2 方式。您应该很快就会注意到不同集成的外观有很多常识。然后尝试以适当的方式调整您想要的内容。然后是进一步提供和分享知识的重要部分:)
同时了解现有的 AngularJS 1.x 解决方案,例如 this article 可能会有所帮助。
1.选项 A(使用 Angular CLI)
在你的 .angular-cli.json 添加 Openlayers3(位于你的项目根目录):
...
"styles": [
"../node_modules/openlayers/dist/ol.css"
],
"scripts": [
"../node_modules/openlayers/dist/ol.js"
],
...
1.选项 B(不适用于 Angular CLI)
在您的 index.html 中添加 Openlayers3("usual" 方式):
<script src="node_modules/openlayers/dist/ol.js"></script>
<link rel="stylesheet" href="node_modules/openlayers/dist/ol.css">
2。从您的打字稿文件访问 ol:
import { AfterViewInit, Component, ElementRef, ViewChild } from "@angular/core";
// This is necessary to access ol3!
declare var ol: any;
@Component({
selector: 'my-app',
template: `
<h3> The map </h3>
<div #mapElement id="map" class="map"> </div>
`
// The "#" (template reference variable) matters to access the map element with the ViewChild decorator!
})
export class AppComponent implements AfterViewInit {
// This is necessary to access the html element to set the map target (after view init)!
@ViewChild("mapElement") mapElement: ElementRef;
public map: any;
constructor(){
var osm_layer: any = new ol.layer.Tile({
source: new ol.source.OSM()
});
// note that the target cannot be set here!
this.map = new ol.Map({
layers: [osm_layer],
view: new ol.View({
center: ol.proj.transform([0,0], 'EPSG:4326', 'EPSG:3857'),
zoom: 2
})
});
}
// After view init the map target can be set!
ngAfterViewInit() {
this.map.setTarget(this.mapElement.nativeElement.id);
}
}
您可以使用 angular2-openlayers,可作为 npm 包或在此处获得:https://github.com/quentin-ol/angular2-openlayers