Angular4抛出错误中的Leaflet Map

时间:2017-11-05 19:37:12

标签: angular typescript leaflet ngx-leaflet

模板:

<div id="mapid" style="height: 500px"></div>

我已经安装了传单和传单的打字。当一个错误显示未找到地图容器时,我添加了它的导入。

控制器:

    import { Component, OnInit, EventEmitter, Output } from 
    '@angular/core';
import * as L from 'leaflet';
import { Map } from 'leaflet';

@Component({
  selector: 'app-leafletmap',
  templateUrl: './leafletmap.component.html',
  styleUrls: ['./leafletmap.component.css']
})
export class LeafletmapComponent implements OnInit {
  mymap = L.map('mapid').setView([29.6516, -82.3248], 13);

  L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token={accessToken}',
   {
    attribution: 'Map data &copy; <a href="http://openstreetmap.org">OpenStreetMap</a> contributors, 
    <a href="http://creativecommons.org/licenses/by-sa/2.0/">CC-BY-SA</a>, Imagery © <a href="http://mapbox.com">Mapbox</a>',
    maxZoom: 18,
    id: 'mapbox.streets',
    accessToken: '*****************************'
  }).addTo(mymap);

  popup = L.popup();
  marker: any;

  onMapClick(e) {
    if (marker != undefined)
      mymap.removeLayer(marker)
    marker = new L.Marker(e.latlng, { draggable: true });
    mymap.addLayer(marker);
    popup.setLatLng(e.latlng).setContent("You clicked the map at " + e.latlng.toString()).openOn(mymap);
  }
  mymap.on('zoomend', function() {
    console.log(mymap.getZoom());
  })
  mymap.on('click', onMapClick);

  constructor() { }

  ngOnInit() {
  }

}

我不确定这是否是通过访问令牌和初始化typescript中的变量的方式,因为我在使用常规Javascript的教程之后编写了此代码。

2 个答案:

答案 0 :(得分:1)

我最近使用传单,所以我发布了完全可行的代码。请自己尝试一下。

我使用下一个npm模块:npm install @asymmetrik/ngx-leaflet --save 看到: https://github.com/Asymmetrik/ngx-leaflet

<强> component.ts

private map: L.Map;

options = {
        layers: [
            L.tileLayer('http://{s}.tile.openstreetmap.org/{z}/{x}/{y}.png', { maxZoom: 15 })
        ],
        zoom: 4,
        center: L.latLng([39.878868, -100.357010])
    };

onMapReady(map: L.Map) {
    this.map = map;
}

<强> component.html

<div leaflet
     [leafletOptions]="options"
     (leafletMapReady)="onMapReady($event)">
</div>

<强> app.module.ts

imports: [
        BrowserModule,
        FormsModule,
        HttpModule,
        AppRoutingModule,
        BrowserAnimationsModule,
        LeafletModule.forRoot() // Don't forget to include leaflet here.
    ]

您可以尝试我的基本变体,然后如果它有效,您可以添加选项和登录onMapReady功能。

UPD:如果你想从传单中引用某些内容,例如:特定组件中的MapOptions,只需以这种方式导入传单。 import * as L from 'leaflet';然后使用L.MapL.MapOptions

UPD2:安装类型npm install @types/leaflet

也很重要

答案 1 :(得分:0)

我收到了与您相同的错误消息:public $details; //or protected $details; )。对我来说,它可以卸载传单类型(Module '"leaflet"' has no exported member 'MapOptions'