首页 > 解决方案 > 错误类型错误:无法读取 ngx-leaflet 和 esri-leaflet-geocoder 上未定义的属性“topleft”

问题描述

我在我的 Angular 9 项目中使用@asymmetrik/ngx-leaflet@asymmetrik/ngx-leaflet-draw用于传单地图。我尝试通过“esri-leaflet-geocoder”在地图中添加搜索选项。没有@asymmetrik/ngx-leaflet@asymmetrik/ngx-leaflet-draw使用我成功地将搜索选项放置在地图中,没有错误。这工作得很好。这是我的工作代码:

/*npm install esri-leaflet esri-leaflet-geocoder*/
import * as L from 'leaflet';
import * as esriGeo from 'esri-leaflet-geocoder';

export class MapComponent implements OnInit {
  public searchControl = new esriGeo.Geosearch();
  
  ngOnInit() {
    this.initMap();        // not all codes are here;
    this.tiles.addTo(L.map);   // not all codes are here;
    
    this.searchControl.addTo(L.map);
  }
}

输出图像: 工作输出 1 工作输出 2

但是当我尝试在之前完成的地方实现相同的代码时@asymmetrik/ngx-leaflet@asymmetrik/ngx-leaflet-draw它会说一些错误:ERROR TypeError: Cannot read property 'topleft' of undefined. 错误输出图像: 在此处输入图像描述

标签: angularleafletgeocodingesringx-leaflet

解决方案


我刚刚解决了这个问题。虽然我正在使用ngx-leaflet所以这里L.Map不是地图的当前实例。从ngx-leaflet初始化地图LeafletDirective。所以工作代码是:

/*npm install esri-leaflet esri-leaflet-geocoder*/
import * as L from 'leaflet';
import * as esriGeo from 'esri-leaflet-geocoder';
import { LeafletDirective } from '@asymmetrik/ngx-leaflet'; //new import

export class MapComponent implements OnInit {
  public searchControl = new esriGeo.Geosearch();
  
  ngOnInit() {
    const map = this.leafletDirective.getMap(); // initialize map
    
    this.searchControl.addTo(map);
  }
}


推荐阅读