简体   繁体   English

在typescript / angular2中使用openlayer3

[英]Using openlayer3 in typescript/angular2

I have a Javascript code which use openLayer3. 我有一个使用openLayer3的Javascript代码。 I need to implement this code in a angular2 project, in Typescript. 我需要在Typescript中的angular2项目中实现此代码。

Someone knows how use openlayer with angular2 / Typescript please ? 有人知道如何使用带有angular2 / Typescript的openlayer吗?

Thanks a lot, 非常感谢,

John 约翰

1. Option A (Working with the Angular CLI) 1.选项A(使用Angular CLI)

Add Openlayers3 in your .angular-cli.json (located at your project root): .angular-cli.json (位于项目根目录)中添加Openlayers3:

...
"styles": [
  "../node_modules/openlayers/dist/ol.css"
],
"scripts": [
  "../node_modules/openlayers/dist/ol.js"
],
...

1. Option B (Not working with the Angular CLI) 1.选项B(不使用Angular CLI)

Add Openlayers3 in your index.html (the "usual" way): index.html中添加Openlayers3(“通常”方式):

<script src="node_modules/openlayers/dist/ol.js"></script> <link rel="stylesheet" href="node_modules/openlayers/dist/ol.css">

2. Access ol from your typescript file: 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);
    }
}

As for the typings you may be interested in DefinitelyTyped project - you can find there openlayers/openlayers.d.ts - you probably need to understand the tsd , folder convetions etc. 至于你可能对DefinitelyTyped项目感兴趣的打字 - 你可以找到openlayers / openlayers.d.ts - 你可能需要了解tsd ,文件夹对流等。

As for the AngularJS 2 considering it's still in beta the experience tells you're mostly on your own. 至于AngularJS 2,考虑到它还处于测试阶段,经验告诉你主要靠自己。 Still googling can point you to ie https://gist.github.com/borchsenius/5a1ec0b48b283ba65021 . 谷歌搜索可以指向你,即https://gist.github.com/borchsenius/5a1ec0b48b283ba65021

Usual approach is first to understand the AngularJS 2 way according to already existing examples. 根据已有的例子,通常的方法是首先理解AngularJS 2方法。 You should soon notice lot of common sense in the way different integrations are looking. 您应该很快就会注意到不同集成方式的常识。 Then try to adapt what you want in appropriate manner. 然后尝试以适当的方式调整你想要的东西。 Then there is the great part to give further and share the knowledge :) 然后有很大的贡献进一步分享知识:)

Also knowing existing AngularJS 1.x solutions like this article may help. 也知道像本文这样的现有AngularJS 1.x解决方案可能有所帮助。

您可以使用angular2-openlayers,可以作为npm包使用,或者在这里: https//github.com/quentin-ol/angular2-openlayers

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM