Ionic 2 Beta和Open Layers 3没有加载地图

时间:2016-03-22 10:48:42

标签: ionic-framework angular openlayers-3 ionic2

我尝试使用新离子2创建一个使用Open Layers 3的测试项目,但它不起作用

这是我的布局:

<ion-navbar *navbar>
  <button menuToggle>
    <ion-icon name="menu"></ion-icon>
  </button>
  <ion-title>Mappa</ion-title>
</ion-navbar>


<ion-content padding class="map-page">

    <div id="map" class="map"></div>

</ion-content>

这是我的控制者:

import {Page} from 'ionic-angular';


@Page({
  templateUrl: 'build/pages/map/map-page.html'
})
export class MapPage {


    constructor(){

        this.test = ["uno", "due", "tre"];



        // var layer = ga.layer.create('ch.swisstopo.pixelkarte-farbe');

     //    var map = new ga.Map({
        //     target: 'map',
        //     layers: [layer],
        //     view: new ol.View({
        //       resolution: 500,
        //       center: [670000, 160000]
        //     })
        //   });

     //    this.map = map;


     console.log(this.map);

      var projection = ol.proj.get('EPSG:3857');

      var map = new ol.Map({
            target: this.map,
            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM()
              })
            ],
            view: new ol.View({
              center: ol.proj.transform([8.92471, 46.07257], 'EPSG:4326', 'EPSG:3857'),
              zoom: 14
            })
          });

    }

}

我还在我的应用程序中导入了这个库:

<!DOCTYPE html>
<html dir="ltr" lang="en">
<head>
  <title>Ionic</title>
  <meta charset="UTF-8">
  <meta name="viewport" content="width=device-width, initial-scale=1.0, minimum-scale=1.0, maximum-scale=1.0, user-scalable=no">
  <meta name="format-detection" content="telephone=no">
  <meta name="msapplication-tap-highlight" content="no">


  <script src="http://openlayers.org/en/v3.14.2/build/ol.js"></script>



  <link ios-href="build/css/app.ios.css" rel="stylesheet">
  <link md-href="build/css/app.md.css" rel="stylesheet">
  <link wp-href="build/css/app.wp.css" rel="stylesheet">  
</head>
<body>

  <ion-app></ion-app>

  <script src="cordova.js"></script>
  <script src="build/js/app.bundle.js"></script>


</body>
</html>

在使用离子1的旧项目中,此代码正常运行。有什么建议吗?

THX。

3 个答案:

答案 0 :(得分:6)

从@Thierry Templier的启发回答我发布关于此问题的完整解决方案:

在index.html上导入标题

<script src="http://openlayers.org/en/v3.14.2/build/ol.js"></script>

手动或通过CLI创建新的自定义组件,这是我的情况:

import {Component, Input, ViewChild, Renderer, Query, QueryList, ElementRef} from 'angular2/core';
import {IONIC_DIRECTIVES} from 'ionic-angular';

declare var ol: any;

@Component({
  selector: 'map-component',
  templateUrl: 'build/components/map-component/map-component.html',
  directives: [IONIC_DIRECTIVES] // makes all Ionic directives available to your component
})




export class MapComponent {

  @ViewChild('map') map;


  constructor(public renderer: Renderer) {


   }

ngAfterViewInit() {
    console.log(this.map);

     var projection = ol.proj.get('EPSG:3857');


     var map = new ol.Map({
            target: "map",
            layers: [
              new ol.layer.Tile({
                source: new ol.source.OSM() 
              })
            ],
            view: new ol.View({
              center: ol.proj.transform([8.92471, 46.07257], 'EPSG:4326', 'EPSG:3857'),
              zoom: 10
            })
          });





 }
}

请记住将此声明为使用OpenLayer库:

declare var ol: any;

然后为自定义组件创建模板或直接在@Component语句中实现该模板,并使用#something与@ViewChild一起使用:

<div id="map" #map class="map"></div>

最后,您可以在页面中使用自定义组件,请记住将其导入页面!

import {Page, NavController} from 'ionic-angular';
import {Query, QueryList, Component, ElementRef} from 'angular2/core';
import {MapComponent} from './../../components/map-component/map-component';


/*
  Generated class for the MapPagePage page.

  See http://ionicframework.com/docs/v2/components/#navigation for more info on
  Ionic pages and navigation.
*/
@Page({
  templateUrl: 'build/pages/map-page/map-page.html',
  directives: [MapComponent]
})

和页面模板文件:

<ion-navbar *navbar>
  <button menuToggle>
    <ion-icon name="menu"></ion-icon>
  </button>
  <ion-title>Map Page</ion-title>
</ion-navbar>

<ion-content padding class="map-page">

  <map-component></map-component>

</ion-content>

那就是它。

答案 1 :(得分:2)

首先需要从模板中引用地图的DOM元素。您可以尝试以下代码:

<ion-navbar *navbar>
  <button menuToggle>
    <ion-icon name="menu"></ion-icon>
  </button>
  <ion-title>Mappa</ion-title>
</ion-navbar>

<ion-content padding class="map-page">

  <div id="map" #map class="map"></div>

</ion-content>

@Query用于您的组件:

@Component({
  (...)
})
class MapComponent {
  constructor(@Query('map') elList: QueryList<ElementRef>) {
    this.map = elList.first;

    (...)

    var projection = ol.proj.get('EPSG:3857');

    var map = new ol.Map({
        target: this.map.nativeElement,
        (...)
    });

    (...)
  }
}

答案 2 :(得分:-1)

简单的解决方案只需使用settimeout!

setTimeout(() => {

            var map = new ol.Map({
                layers: [
                    new ol.layer.Tile({
                        source: new ol.source.OSM()
                    })
                ],
                target: 'openlayers',
                controls: ol.control.defaults({
                    attributionOptions: ({
                        collapsible: false
                    })
                }),
                view: new ol.View({
                    center: [0, 0],
                    zoom: 2
                })
            });

        }, 500);