Angular引入百度地图js

第一步:申请百度地图密钥,很简单,去网上随便找教程

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

第三部,创建一个组件

html部分

<div id = "map" style="width:100%;height: 100%"></div>
js部分

import { Component,OnInit } from '@angular/core';
import { NavController,NavParams } from 'ionic-angular';
declare var BMap: any;
@Component({
  selector: 'page-map',
  templateUrl: 'map.html'
})
export class MapPage {
  thisX = "";
  thisY = "";
  constructor(public navCtrl: NavController,public params:NavParams) {
    this.thisX = this.params.get('x');
    this.thisY = this.params.get('y');
  }
  ngOnInit() {
    const map = new BMap.Map('map');//创建地图实例
    const point = new BMap.Point(this.thisX,this.thisY);//创建点坐标
    map.centerAndZoom(point, 15);//初始化地图,设置中心点坐标和地图级别
    map.enableScrollWheelZoom(true);//开启鼠标滚轮缩放
    console.log(map);
  }
}
 

运行就可以看到地图了

猜你喜欢

转载自blog.csdn.net/liuguochao1024/article/details/83621863