【问题标题】:Ionic 2 Beta and Open Layers 3 not loading mapIonic 2 Beta 和 Open Layers 3 未加载地图
【发布时间】:2016-03-22 10:48:42
【问题描述】:

我已尝试创建一个测试项目以将 Open Layers 3 与新的 ionic 2 一起使用,但它不起作用

这是我的布局:

<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>

在具有 ionic 1 的旧项目中,此代码正在运行。有什么建议吗?

谢谢。

【问题讨论】:

    标签: ionic-framework angular openlayers-3 ionic2


    【解决方案1】:

    受@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>
    

    就是这样。

    【讨论】:

      【解决方案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,
              (...)
          });
      
          (...)
        }
      }
      

      【讨论】:

      • 您好,谢谢您的回答。我在这方面做了一点工作。事实上,我受到您的建议的启发,但没有一些重要的部分。我会用完整的解决方案回答自己。
      【解决方案3】:

      您好,简单的解决方案只需使用 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);
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2016-12-15
        • 1970-01-01
        • 1970-01-01
        相关资源
        最近更新 更多