Javascript 映射框标记抛出类型错误
我正在尝试从mapbox向地图添加标记 这是我的角度打字脚本代码Javascript 映射框标记抛出类型错误,javascript,angular,typescript,mapbox-gl-js,Javascript,Angular,Typescript,Mapbox Gl Js,我正在尝试从mapbox向地图添加标记 这是我的角度打字脚本代码 export class MappViewComponent implements OnInit { map: mapboxgl.Map; lat = 41.1293; lng = -8.4464; style = "mapbox://styles/mapbox/streets-v11"; zoom = 8; constructor( private mapService: M
export class MappViewComponent implements OnInit {
map: mapboxgl.Map;
lat = 41.1293;
lng = -8.4464;
style = "mapbox://styles/mapbox/streets-v11";
zoom = 8;
constructor(
private mapService: MapService,
private nodeService: NodeService
) {}
ngOnInit() {
this.buildMap(this.map);
/*var mymap = L.map('mapID').setView([41.260555, -8.436098], 10);
this.buildMap(mymap);*/
this.addMarkersLines(this.map);
new mapboxgl.Marker().setLngLat([this.lng, this.lat]).addTo(this.map);
}
/**https://leafletjs.com/reference-1.7.1.html */
buildMap(map: any) {
map = new mapboxgl.Map({
accessToken:
"accessToken",
container: "mapID",
style: this.style,
zoom: this.zoom,
center: [this.lng, this.lat],
antialias: true,
attributionControl: false,
});
map.addControl(new mapboxgl.NavigationControl());
问题是,在console中运行后,它会在这一行抛出此错误:
new mapboxgl.Marker().setLngLat([this.lng, this.lat]).addTo(this.map);
错误类型错误:t未定义
添加到地图框总图:35
ngOnInit地图视图.组件.ts:30
有人有这种问题吗?最有可能发生错误,因为
此地图未定义
new mapboxgl.Marker().setLngLat([this.lng, this.lat]).addTo(this.map);
^^^^^^^^
undefined
函数buildMap
的实现方式不会影响传递的对象,这意味着在您的情况下,this.map
保持未定义。尝试重构buildMap
函数,例如:
buildMap() {
this.map = new mapboxgl.Map({
accessToken: MAPBOX_TOKEN,
container: 'mapID',
style: this.style,
zoom: this.zoom,
center: [this.lng, this.lat],
antialias: true,
attributionControl: false,
});
this.map.addControl(new mapboxgl.NavigationControl());
}
用法
ngOnInit() {
this.buildMap();
new mapboxgl.Marker().setLngLat([this.lng, this.lat]).addTo(this.map);
}