- 注册百度账号
- 申请成为百度开发者
- 获取密钥(AK)
index.html文件
// head标签中导入
<script type="text/javascript" src="//api.map.baidu.com/api?type=webgl&v=1.0&ak=你申请的密钥"></script>
map.component.html文件
<div #mapContainer style="width: 100%; height:100%"></div>
map.component.ts文件
import { OnInit, AfterViewInit, ElementRef, ViewChild } from '@angular/core';
@Component({
selector: 'map',
templateUrl: './map.component.html',
styleUrls: ['./map.component.scss']
})
export class MapComponent implements OnInit, AfterViewInit {
@ViewChild('mapContainer') mapDom!: ElementRef<HTMLElement>
constructor() { }
ngOnInit(): void { };
ngAfterViewInit(): void {
let GL = (window as any).BMapGL;
let map = new GL.Map(this.mapDom.nativeElement);
map.centerAndZoom('上海市', 12); // 初始化地图,设置中心点坐标和地图级别
map.enableScrollWheelZoom(true); // 设置鼠标滑轮缩放
}
}