ERROR TypeError: Cannot read property 'topleft' of undefined on ngx-leaflet and esri-leaflet-geocoder
ERROR TypeError: Cannot read property 'topleft' of undefined on ngx-leaflet and esri-leaflet-geocoder
我在 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);
}
}
输出图像:
但是当我尝试在之前完成 @asymmetrik/ngx-leaflet
和 @asymmetrik/ngx-leaflet-draw
的地方实现相同的代码时,它说一些错误: ERROR TypeError: Cannot read property 'topleft' of undefined
。
错误输出图像:
我刚刚解决了这个问题。虽然我使用的是 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);
}
}
我在 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);
}
}
输出图像:
但是当我尝试在之前完成 @asymmetrik/ngx-leaflet
和 @asymmetrik/ngx-leaflet-draw
的地方实现相同的代码时,它说一些错误: ERROR TypeError: Cannot read property 'topleft' of undefined
。
错误输出图像:
我刚刚解决了这个问题。虽然我使用的是 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);
}
}