• 欢迎访问搞代码网站,推荐使用最新版火狐浏览器和Chrome浏览器访问本网站!
  • 如果您觉得本站非常有看点,那么赶紧使用Ctrl+D 收藏搞代码吧

详解在Angular4中使用ng2-baidu-map的方法

angularjs 搞代码 4年前 (2021-12-31) 30次浏览 已收录 0个评论

这篇文章主要介绍了在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.来源gaodai#ma#com搞@@代~&码网在app.component.html使用

 <div style="height: 500px;width: 900px">  <!----><!--导航控件--><!--地图全景控件--><!--比例尺--><!--地图类型--></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; 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, // 是否启用键盘移动地图功能 enableInertialDragging: false,   // 是否启用惯性阻力函数 enableContinuousZoom: true, // 是否启用连续缩放功能 disablePinchToZoom: false,  // 是否禁用缩放功能的缩放 cursor: '',     // 设置默认的光标样式,应该遵循CSS规范 draggingCursor: '', // 设置默认的拖动光标样式,应该遵循CSS规范 currentCity: '苏州市',  // 设置当前的城市 }; // 这是地图标记marker this.markers = [ { options: { icon: { imageUrl: '/assets/1.jpg-600', size: { height: 60, width: 50 } }, title: 'asdkjgaslfkjasd' }, point: { lng: 120.62,  // 经度 lat: 31.32,  // 纬度 } }, { point: { lng: 120.63,  // 经度 lat: 31.32,  // 纬度 } }, { point: { lng: 120.63,  // 经度 lat: 31.31,  // 纬度 } } ]; // 这是控件control this.controlOpts = {     // 导航控件 anchor: ControlAnchor.BMAP_ANCHOR_TOP_LEFT,   // 显示的控件的位置 type: NavigationControlType.BMAP_NAVIGATION_CONTROL_LARGE,  // 用来描述它是什么样的导航 offset: {                    // 控件的大小 width: 30, height: 30 }, showZoomInfo: true,               // 是否展示当前的信息 enableGeolocation: true             // 是否启用地理定位功能 }; this.overviewmapOpts = {  // 地图全景控件 anchor: ControlAnchor.BMAP_ANCHOR_BOTTOM_RIGHT, // 显示的控件的位置 isOpen: true }; this.scaleOpts = {     // 比例尺控件 anchor: ControlAnchor.BMAP_ANCHOR_BOTTOM_LEFT }; this.mapTypeOpts = {    // 地图类型 type: MapTypeControlType.BMAP_MAPTYPE_CONTROL_HORIZONTAL }; // Geolocation 和Panorama 没有属性 } }

效果预览

总结

如果你觉得本文对你有帮助,欢迎转载,烦请注明出处,谢谢!

以上就是详解在Angular4中使用ng2-baidu-map的方法的详细内容,更多请关注gaodaima搞代码网其它相关文章!


搞代码网(gaodaima.com)提供的所有资源部分来自互联网,如果有侵犯您的版权或其他权益,请说明详细缘由并提供版权或权益证明然后发送到邮箱[email protected],我们会在看到邮件的第一时间内为您处理,或直接联系QQ:872152909。本网站采用BY-NC-SA协议进行授权
转载请注明原文链接:详解在Angular4中使用ng2-baidu-map的方法

喜欢 (0)
[搞代码]
分享 (0)
发表我的评论
取消评论

表情 贴图 加粗 删除线 居中 斜体 签到

Hi,您需要填写昵称和邮箱!

  • 昵称 (必填)
  • 邮箱 (必填)
  • 网址