设为首页 加入收藏

TOP

在Angular4中使用ng2-baidu-map详解(一)
2019-09-17 18:48:04 】 浏览:42
Tags:Angular4 使用 ng2-baidu-map 详解

一、引言

之前在Angular4使用过百度地图,记录一下踩过的坑

二、实现

1.安装

npm install angular2-baidu-map

2.在app.module.ts配置

ak key在http://lbsyun.baidu.com/apiconsole/key中创建

import { BrowserModule } from '@angular/platform-browser'
import { NgModule } from '@angular/core'

import { AppComponent } from './app.component'

import { BaiduMapModule } from 'angular2-baidu-map'

@NgModule({
  declarations: [AppComponent],
  imports: [BrowserModule, BaiduMapModule.forRoot({ ak: 'your ak' })],
  providers: [],
  bootstrap: [AppComponent]
})
export class AppModule {}

3.在app.component.html使用

<div style="height: 500px;width: 900px;" >
  <baidu-map [options]="opts" >
    <!--<marker [point]="point" [options]="markOpts" (loaded)="" (clicked)=""></marker>-->
    <marker *ngFor="let marker of markers" [point]="marker.point" [options]="marker.options"></marker>
    <!--导航控件-->
    <control type="navigation" [options]="controlOpts"></control>
    <!--地图全景控件-->
    <control type="overviewmap" [options]="overviewmapOpts"></control>
    <!--比例尺-->
    <control type="scale" [options]="scaleOpts"></control>
    <!--地图类型-->
    <control type="maptype" [options]="mapTypeOpts"></control>
 
    <control type="geolocation" [options]="geolocationOpts"></control>
  </baidu-map>
</div>

4.在app.component.ts

import {Component, OnInit} from '@angular/core';
 
import {
  MapOptions, Point, MarkerOptions, NavigationControlOptions, ControlAnchor,
  NavigationControlType, OverviewMapControlOptions, ScaleControlOptions, MapTypeControlOptions, MapTypeControlType,
  GeolocationControlOptions
} from 'angular2-baidu-map';
@Component({
  selector: 'app-root',
  templateUrl: './app.component.html',
  styleUrls: ['./app.component.sass']
})
export class AppComponent {
 
  public opts: MapOptions;
  public markers: Array<{ point: Point; options?: MarkerOptions }>;
  public controlOpts: NavigationControlOptions;
  public overviewmapOpts: OverviewMapControlOptions;
  public scaleOpts: ScaleControlOptions;
  public mapTypeOpts: MapTypeControlOptions;
  public geolocationOpts: GeolocationControlOptions;
  // 文字标注
  public text: string;
  public onMarkerLoad(marker: any) {
    const label = new window.BMap.Label(’文字标注‘, {
      offset: new window.BMap.Size(20, -12)
    });
    label.setStyle({
      border: '1px solid #d81e06',
      color: '#d81e06',
      fontSize: '10px',
      padding: '1px'
    });
    marker.setLabel(label);
  }
  constructor() {
    this.opts = {
      centerAndZoom: {     // 设置中心点和缩放级别
        lng: 120.62,   // 经度
        lat: 31.32,    // 纬度
        zoom: 15           // 缩放级别
      },
      minZoom: 3,  // 最小缩放级别的地图
      maxZoom: 19, // 最大缩放级别的地图
      enableHighResolution: true,  // 是否用高分辨率的地图,default:true
      enableAutoResize: true,  // 是否可以自动调整大小,default:true
      enableMapClick: true,  // 地图是否可以点击,default:true
      disableDragging: false, // 是否禁用地图拖动功能
      enableScrollWheelZoom: true, // 是否启用滚轮进行缩放功能
      disableDoubleClickZoom: false, // 是否禁用双击缩放功能
      enableKeyboard: true,  // 是否启用键盘移动地图功能
      enab
首页 上一页 1 2 下一页 尾页 1/2/2
】【打印繁体】【投稿】【收藏】 【推荐】【举报】【评论】 【关闭】 【返回顶部
上一篇前端微服务-面向web平台级应用的.. 下一篇Vue 生命周期

最新文章

热门文章

Hot 文章

Python

C 语言

C++基础

大数据基础

linux编程基础

C/C++面试题目