Leaflet JS TypeError:无法读取null的属性“ addLayer”

时间:2019-02-13 15:29:50

标签: angular leaflet mapbox

我正在Leaflet JS + Mapbox上创建地图。尝试以视觉方式突出显示我的区域。我有代码,这些代码通过我创建的geoJSON覆盖了我的区域。但是,当我编写代码以突出显示Mousover上的区域时,我遇到TypeError:无法读取null的属性'addLayer'。

我的代码

尝试使用传单文档重构我的代码。

import {Component, ElementRef, OnInit, ViewChild} from '@angular/core';
import {Subscription} from 'rxjs';
import {MapService} from '../../src/app/core/shared/services/map.service'

import * as L from 'leaflet';
import 'leaflet-routing-machine';
import 'leaflet-providers';
import {style} from "@angular/animations";

@Component({
  selector: 'app-root',
  templateUrl: './map.component.html',
  styleUrls: ['./map.component.css']
})
export class MapComponent implements OnInit {

  @ViewChild('mapWrapper')
  mapWrapper: ElementRef;

  zoom = 6;

  /* Regions */
  regionsGeoJsonSubscription$: Subscription;
  regionsGeoJson: any;


  constructor(private mapService: MapService) {
  }

  title = 'leaflet-routing-sample';

  private map: L.Map = null;
  private geojson = null;


  style(featurecollection) {
    return {
      fillColor: '#99d8c9' ,
      weight: 2,
      opacity: 1,
      color: 'white',
      dashArray: '3',
      fillOpacity: 0.7
    };
  }

  ngOnInit() {

    this.regionsGeoJsonSubscription$ = this.mapService.getRegionsGeoJson()
      .subscribe(regionsGeoJson => {
          this.regionsGeoJson = regionsGeoJson;
          //this.regionsGeoJsonLoaded = Object.keys(this.regionsGeoJson).length > 0;
          //this.statisticsEnabled = true;
        this.map = L.map('map')
          .setView([55.744100, 37.627027], 13);

        L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=mytoken', {
          id: 'mapbox.light',
          attribution: null
        }).addTo(this.map)

        L.geoJson(this.regionsGeoJson, {style: this.style}).addTo(this.map);
        }
      );

    function highlightFeature(e) {
      this.layer = e.target;

      this.layer.setStyle({
        weight: 5,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
      });

      if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
        this.layer.bringToFront();
      }
    }

    function resetHighlight(e) {
      this.geojson.resetStyle(e.target);
    }

    function zoomToFeature(e) {
      this.map.fitBounds(e.target.getBounds());
    }



    function onEachFeature(feature, layer) {
      this.layer.on({
        mouseover: highlightFeature,
        mouseout: resetHighlight,
        click: zoomToFeature
      });
    }

    this.geojson = L.geoJson(this.regionsGeoJson, {
      style: style,
      onEachFeature: onEachFeature
    }).addTo(this.map);

  }

}

My map and DevTools

2 个答案:

答案 0 :(得分:0)

onEachFeature之前,您拥有:

this.geojson = L.geoJson(this.regionsGeoJson, {
    style: style,
    onEachFeature: onEachFeature
}).addTo(this.map);

此刻,此映射尚未初始化并且为空,并且当您调用addTo方法时,它在内部执行map.addLayer(this);

编辑:

您错过了onEachFeature函数中的某些东西:

function onEachFeature(feature, layer) {
    layer.on({
        mouseover: highlightFeature,
        mouseout: resetHighlight,
        click: zoomToFeature
    });
}

它是layer.on而不是this.layer.on,因为当前类中没有任何变量图层,这是传递给函数的图层变量。

答案 1 :(得分:0)

感谢Julien Metral,他将我引到了我的解决方案中。因此,这里是:

ngOnInit() {

this.regionsGeoJsonSubscription$ = this.mapService.getRegionsGeoJson()
  .subscribe(regionsGeoJson => {
      this.regionsGeoJson = regionsGeoJson;
    this.map = L.map('map')
      .setView([55.744100, 37.627027], 6);

    function highlightFeature(e) {

      const layer = e.target;

      layer.setStyle({
        weight: 5,
        color: '#666',
        dashArray: '',
        fillOpacity: 0.7
      });

      if (!L.Browser.ie && !L.Browser.opera && !L.Browser.edge) {
        layer.bringToFront();
      }
    }

    function resetHighlight(e) {
      this.geojson.resetStyle(e.target);
    }

    function zoomToFeature(e) {
      this.map.fitBounds(e.target.getBounds());
    }


    function onEachFeature(feature, layer) {
      layer.on({
        mouseover: highlightFeature,
        mouseout: resetHighlight,
        click: zoomToFeature
      });
    }

    this.geojson = L.geoJson(this.regionsGeoJson, {
      style: this.style,
      onEachFeature: onEachFeature
    }).addTo(this.map);

    L.tileLayer('https://api.tiles.mapbox.com/v4/{id}/{z}/{x}/{y}.png?access_token=mytoken', {
      id: 'mapbox.light',
      attribution: null
    }).addTo(this.map)

    }
  );
}