Mapbox-gl.js 地图在加载事件的 angular-cli 项目中未定义
Mapbox-gl.js map is undefined in angular-cli project on load event
启动了一个 angular-cli 项目并通过 npm 安装了 mapbox-gl。在 angular-cli.json 文件中包含 mapbox-gl 脚本和样式。我可以显示地图,但无法在加载事件中添加图层。地图突然未定义?这是模板 html (app.component.html):
<div id="map" style="margin:0; padding:0; position:absolute; top:25px; bottom:0; width:100%;">
</div>
这是我的组件代码 (app.component.ts):
import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core';
import * as mapboxgl from 'mapbox-gl';
import { Map } from 'mapbox-gl';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild("map") map: Map;
title = 'app';
ngOnInit() {
mapboxgl.accessToken = 'blah';
this.map = new Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 5,
center: [-78.880453, 42.897852]
});
this.map.on('load', this.onLoad);
}
onLoad(){
console.log("map is loaded, can I still talk to it?");
this.map.addLayer({
"id": "points",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-77.03238901390978, 38.913188059745586]
},
"properties": {
"title": "Mapbox DC",
"icon": "monument"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.414, 37.776]
},
"properties": {
"title": "Mapbox SF",
"icon": "harbor"
}
}]
}
},
"layout": {
"icon-image": "{icon}-15",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}
});
}
}
尝试使用任一bind
方法
this.map.on('load', this.onLoad.bind(this));
或局部箭头函数
this.map.on('load', () => this.onLoad());
或实例箭头函数
load = () => {
...
这样this
将引用组件实例
启动了一个 angular-cli 项目并通过 npm 安装了 mapbox-gl。在 angular-cli.json 文件中包含 mapbox-gl 脚本和样式。我可以显示地图,但无法在加载事件中添加图层。地图突然未定义?这是模板 html (app.component.html):
<div id="map" style="margin:0; padding:0; position:absolute; top:25px; bottom:0; width:100%;">
</div>
这是我的组件代码 (app.component.ts):
import { Component, ViewChild, AfterViewInit, OnInit } from '@angular/core';
import * as mapboxgl from 'mapbox-gl';
import { Map } from 'mapbox-gl';
@Component({
selector: 'app-root',
templateUrl: './app.component.html',
styleUrls: ['./app.component.css']
})
export class AppComponent implements OnInit {
@ViewChild("map") map: Map;
title = 'app';
ngOnInit() {
mapboxgl.accessToken = 'blah';
this.map = new Map({
container: 'map',
style: 'mapbox://styles/mapbox/light-v9',
zoom: 5,
center: [-78.880453, 42.897852]
});
this.map.on('load', this.onLoad);
}
onLoad(){
console.log("map is loaded, can I still talk to it?");
this.map.addLayer({
"id": "points",
"type": "symbol",
"source": {
"type": "geojson",
"data": {
"type": "FeatureCollection",
"features": [{
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-77.03238901390978, 38.913188059745586]
},
"properties": {
"title": "Mapbox DC",
"icon": "monument"
}
}, {
"type": "Feature",
"geometry": {
"type": "Point",
"coordinates": [-122.414, 37.776]
},
"properties": {
"title": "Mapbox SF",
"icon": "harbor"
}
}]
}
},
"layout": {
"icon-image": "{icon}-15",
"text-field": "{title}",
"text-font": ["Open Sans Semibold", "Arial Unicode MS Bold"],
"text-offset": [0, 0.6],
"text-anchor": "top"
}
});
}
}
尝试使用任一bind
方法
this.map.on('load', this.onLoad.bind(this));
或局部箭头函数
this.map.on('load', () => this.onLoad());
或实例箭头函数
load = () => {
...
这样this
将引用组件实例