Angular引入百度地圖教程

下面簡單介紹一下如何在Angular中使用百度地圖。

第一步:申請百度地圖密鑰(http://lbsyun.baidu.com/index.php?title=jspopular/guide/getkey);

第二步:在Angular項目中引入百度地圖API文件,在index.html中引入

<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=您的密鑰"></script>

(注意,百度地圖js必須在根組件index.html頁面引入,否則會報錯)
另外,如果需要去除百度地圖左下角的logo,僅需要在styles.scss中添加如下代碼就可以了。

.anchorBL{
  display:none;
}

在component.ts文件中:

import { Component, OnInit } from '@angular/core';
declare var BMap: any;
@Component({
  selector: 'app-demo',
  templateUrl: './demo.component.html',
  styleUrls: ['./demo.component.scss']
})
export class DemoComponent implements OnInit {
  constructor() {}
  ngOnInit() {
    const map = new BMap.Map('map');//創建地圖實例
    const point = new BMap.Point(116.404, 39.915);//創建點座標
    map.centerAndZoom(point, 15);//初始化地圖,設置中心點座標和地圖級別
    map.enableScrollWheelZoom(true);//開啓鼠標滾輪縮放
  }
}

第四步:運行項目,就可以看到所引入的地圖啦.
轉載https://www.itsvse.com/thread-7481-1-1.html

發表評論
所有評論
還沒有人評論,想成為第一個評論的人麼? 請在上方評論欄輸入並且點擊發布.
相關文章