当前位置: 首页 > 工具软件 > angular-BMap > 使用案例 >

angular.4 引入百度地图

岳英耀
2023-12-01

1、在index.html中引入 bmap.min.js

  <script type="text/javascript" src="./assets/echarts/bmap.min.js"></script>
  <script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=自己申请的密钥"></script>

2、在使用地图的组件中声明(在引入文件的下面)

  import {xxx } from './xxx/xxx'
  
  declare var BMap: any

3、在 ngOnInit(){}中初始化

   this.map = new BMap.Map("container", {
		enableMapClick: false
	});                             // 创建Map实例
	this.map.centerAndZoom(new BMap.Point(105.403119, 38.028658), 5);  // 初始化地图,设置中心点坐标和地图级别
	this.map.enableScrollWheelZoom(true); // 开启鼠标滚轮缩放

   这样页面就会出现地图了

4、 添加标点的点击事件
// 在接口里获取的数据 (标点的位置)

   x 为接口里的数据 (可修改为自己的数据)
   let point = new BMap.Point(x.lng, x.lat);
   let marker = new BMap.Marker(point);
   this.map.addOverlay(marker);
   this.opts.title = x.projectName;
	// 位置的相应的信息
	let infoWindow = new BMap.InfoWindow('地址:' + x.address + '<br/>状态:' + x.workStatus + '--' + x.liftFloor + '--' +
		x.lossloadStatus + '/' + x.overloadStatus, this.opts);
		
	//  点击标点时 弹出框 展示对应的信息
	marker.addEventListener('click', function () {
	     this.map.openInfoWindow(this.infoWindow, point); // 开启信息窗口
    });

5、根据项目的要求 改变地图的背景颜色 (为自己设置的颜色 #02021a )

    //消除底图兴趣点标记
	this.map.setMapStyle({
		styleJson: [
			{
				"featureType": "poilabel",
				"elementType": "labels.icon",
				"stylers": {
					"weight": "1",
					"lightness": -70,
					"saturation": 1,
					"visibility": "off"
				}
			},
			{
				"featureType": "land",
				"elementType": "geometry",
				"stylers": {
					"color": "#02021a "       //==修改自己的颜色==
				}
			},{
				"featureType": "manmade",
				"elementType": "geometry",
				"stylers": {
					"color": "#02021a"    //修改自己的颜色
				}
			}, {
				"featureType": "water",
				"elementType": "geometry",
				"stylers": {
					"color": "#17263cff"
				}
			}, {
				"featureType": "road",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#9e7d60ff"
				}
			}, {
				"featureType": "road",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#554631ff"
				}
			}, {
				"featureType": "districtlabel",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#d69563ff"
				}
			}, {
				"featureType": "districtlabel",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#17263cff",
					"weight": 3
				}
			}, {
				"featureType": "poilabel",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#d69563ff"
				}
			}, {
				"featureType": "poilabel",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#17263cff",
					"weight": 3
				}
			}, {
				"featureType": "subway",
				"elementType": "geometry",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "railway",
				"elementType": "geometry",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "poilabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "subwaylabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "subwaylabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "tertiarywaysign",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "tertiarywaysign",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "provincialwaysign",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "provincialwaysign",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "nationalwaysign",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "nationalwaysign",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "highwaysign",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "highwaysign",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "green",
				"elementType": "geometry",
				"stylers": {
					"color": "#263b3eff"
				}
			}, {
				"featureType": "nationalwaysign",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#d0021bff"
				}
			}, {
				"featureType": "nationalwaysign",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#ffffffff"
				}
			}, {
				"featureType": "city",
				"elementType": "labels",
				"stylers": {
					"visibility": "on"
				}
			}, {
				"featureType": "city",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "city",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#d69563ff"
				}
			}, {
				"featureType": "city",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#17263cff"
				}
			}, {
				"featureType": "water",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#d69563ff"
				}
			}, {
				"featureType": "water",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#242f3eff"
				}
			}, {
				"featureType": "local",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#38414eff"
				}
			}, {
				"featureType": "local",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#ffffff00"
				}
			}, {
				"featureType": "fourlevelway",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#38414eff"
				}
			}, {
				"featureType": "fourlevelway",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#ffffff00"
				}
			}, {
				"featureType": "tertiaryway",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#38414eff"
				}
			}, {
				"featureType": "tertiaryway",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#ffffff00"
				}
			}, {
				"featureType": "tertiaryway",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#759879ff"
				}
			}, {
				"featureType": "fourlevelway",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#759879ff"
				}
			}, {
				"featureType": "highway",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#759879ff"
				}
			}, {
				"featureType": "highway",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#9e7d60ff"
				}
			}, {
				"featureType": "highway",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#554631ff"
				}
			}, {
				"featureType": "provincialway",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#9e7d60ff"
				}
			}, {
				"featureType": "provincialway",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#554631ff"
				}
			}, {
				"featureType": "tertiaryway",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#1a2e1cff"
				}
			}, {
				"featureType": "fourlevelway",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#1a2e1cff"
				}
			}, {
				"featureType": "highway",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#1a2e1cff"
				}
			}, {
				"featureType": "nationalway",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#1a2e1cff"
				}
			}, {
				"featureType": "nationalway",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#759879ff"
				}
			}, {
				"featureType": "nationalway",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#9e7d60ff"
				}
			}, {
				"featureType": "nationalway",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#554631ff"
				}
			}, {
				"featureType": "cityhighway",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#9e7d60ff"
				}
			}, {
				"featureType": "cityhighway",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#554631ff"
				}
			}, {
				"featureType": "arterial",
				"elementType": "geometry.fill",
				"stylers": {
					"color": "#9e7d60ff"
				}
			}, {
				"featureType": "arterial",
				"elementType": "geometry.stroke",
				"stylers": {
					"color": "#554631fa"
				}
			}, {
				"featureType": "medicallabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "medicallabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "entertainmentlabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "entertainmentlabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "estatelabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "estatelabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "businesstowerlabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "businesstowerlabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "companylabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "companylabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "governmentlabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "governmentlabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "restaurantlabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "restaurantlabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "hotellabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "hotellabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "shoppinglabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "shoppinglabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "lifeservicelabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "lifeservicelabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "carservicelabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "carservicelabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "financelabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "financelabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "otherlabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "otherlabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			}, {
				"featureType": "airportlabel",
				"elementType": "labels",
				"stylers": {
					"visibility": "on"
				}
			}, {
				"featureType": "airportlabel",
				"elementType": "labels.text.fill",
				"stylers": {
					"color": "#d69563ff"
				}
			}, {
				"featureType": "airportlabel",
				"elementType": "labels.text.stroke",
				"stylers": {
					"color": "#17263cff"
				}
			}, {
				"featureType": "airportlabel",
				"elementType": "labels.icon",
				"stylers": {
					"visibility": "off"
				}
			},
		]
	});

5、

 类似资料: