主要分解为如下步骤:

(1)在html文件中引入百度地图,
<script type="text/javascript" src="http://api.map.baidu.com/api?v=2.0&ak=your token "></script>
 
(2)在webpack.base.conf.js文件内添加external选项,在module.exports内部,和entry平级;
externals: {
   "BMap": "BMap"

 }, 

 
(3)添加地图组件BMapComponent.vue,这里主要注意两点:
    a)使用BMap的时候不需要import了,import反而会报错     
    b)注意一定要给bmap的div设置高度,否则会看不见
该组件的代码如下
  1. <template>
  2. <!--<div id="allmap" style="width: 100%; height: {{mapHeight}}px"></div>-->
  3. <!--<div id="allmap":style="{width: '100%', height: mapHeight + 'px'}"></div>-->
  4. <div id="allmap" v-bind:style="mapStyle"></div>
  5. </template>
  6. <script>
  7. exportdefault{
  8. data:function(){
  9. return{
  10. mapStyle:{
  11. width:'100%',
  12. height:this.mapHeight +'px'
  13. }
  14. }
  15. },
  16. props:{
  17. // 地图在该视图上的高度
  18. mapHeight:{
  19. type:Number,
  20. default:500
  21. },
  22. // 经度
  23. longitude:{
  24. type:Number,
  25. default:116.404
  26. },
  27. // 纬度
  28. latitude:{
  29. type:Number,
  30. default:39.915
  31. },
  32. description:{
  33. type:String,
  34. default:'天安门'
  35. }
  36. },
  37. ready:function(){
  38. var map =newBMap.Map("allmap");
  39. var point =newBMap.Point(this.longitude,this.latitude);
  40. var marker =newBMap.Marker(point);
  41. map.addOverlay(marker);
  42. map.centerAndZoom(point,15);
  43. // 信息窗的配置信息
  44. var opts ={
  45. width :250,
  46. height:75,
  47. title :"地址:",
  48. }
  49. var infoWindow =newBMap.InfoWindow(this.description, opts);// 创建信息窗口对象
  50. marker.addEventListener("click",function(){
  51. map.openInfoWindow(infoWindow,point);
  52. });
  53. map.enableScrollWheelZoom(true);
  54. map.openInfoWindow(infoWindow,point);//开启信息窗口
  55. }
  56. }
  57. </script>
  58. <!--Add"scoped" attribute to limit CSS to this component only -->
  59. <style scoped>
  60. </style>
 
(4) 在父组件中使用
    a)引入 import BMapComponent from './components/BMapComponent.vue'
    b)在template中增加标签
     <b-map-component></b-map-component>
    注意这里标签的命名,在vue文档中有说:http://vuejs.org.cn/guide/components.html#资源命名约定 
 

 

Vue系列:如何将百度地图包装成Vue的组件的更多相关文章

  1. vue项目中使用百度地图的方法

    1.在百度地图申请密钥: http://lbsyun.baidu.com/  将 <script type="text/javascript" src="http: ...

  2. 使用Vue Baidu Map对百度地图实现输入框搜索定位

    前端时间需要在页面的输入框输入地址,搜索并在百度地图上获取选定结果的坐标,前端使用了Vue + Element-ui,地图方面直接使用了封装好的百度地图vue组件-vue-baidu-map     ...

  3. 微信小程序--百度地图坐标转换成腾讯地图坐标

    最近开发小程序时出现一个问题,后台程序坐标采用的时百度地图的坐标,因为小程序地图时采用的腾讯地图的坐标系,两种坐标有一定的误差,导致位置信息显示不正确.现在需要一个可以转换两种坐标的方法,经过查询发现 ...

  4. vue 项目中引用百度地图

    新建map.js export const BaiduMap = { init: function() { const BMapURL = 'https://api.map.baidu.com/api ...

  5. 在Vue框架中使用百度地图

    1.首先在index.html中引入百度地图 <script type="text/javascript" src="http://api.map.baidu.co ...

  6. Vue中加载百度地图

    借助百度地图的 LocalSearch 和 Autocomplete 两个方法 实现方式:通过promise以及百度地图的callback回调函数 map.js 1 export function M ...

  7. Vue系列教程(一)之初识Vue

    一.Vue和MVVM Vue是一个渐进式的js框架,只注重视图层,结合了HTML+CSS+JS,非常的易用,并且有很好的生态系统,而且vue体积很小,速度很快,优化很到位. Vue技术周四MVVM开发 ...

  8. Vue系列:在vux的popup组件中使用百度地图遇到显示不全的问题

    问题描述: 将百度地图封装成一个独立的组件BMapComponent,具体见 Vue系列:如何将百度地图包装成Vue的组件(http://www.cnblogs.com/strinkbug/p/576 ...

  9. Qt的QWebChannel和JS、HTML通信/交互驱动百度地图

    Qt的QWebChannel和JS.HTML通信/交互驱动百度地图 0 前言 我一个研究嵌入式的,不知道怎么就迷上了上位机,接了几个项目都是关于Qt,这个项目还是比较经典的,自己没事儿的时候也进行研究 ...

随机推荐

  1. DevExpress ASPxSplitter ClientSideEvents-PaneExpanded 时间用法

    在webform中的devexpress aspxsplitter中套用了jquery-easyui的 tabs id=tt <div id="tt" class=" ...

  2. Maven基础使用

    常用命令 mvn clean:清除maven的编译结果 mvn compile:编译 mvn package:编译.打包 mvn install:编译.打包.部署 –DskipTests:编译测试用例 ...

  3. JavaScript Patterns 6.6 Mix-ins

    Loop through arguments and copy every property of every object passed to the function. And the resul ...

  4. 如何分析解读systemstat dump产生的trc文件

    ORACLE数据库的systemstat dump生成trace文件虽然比较简单,但是怎么从trace文件中浩如烟海的信息中提炼有用信息,并作出分析诊断是一件技术活,下面收集.整理如何分析解读syst ...

  5. BootStrap入门教程 (一)

    BootStrap入门教程 (一)   2011年,twitter的"一小撮"工程师为了提高他们内部的分析和管理能力,用业余时间为他们的产品构建了一套易用.优雅.灵活.可扩展的前端 ...

  6. Jade模板引擎(一)之Attributes

    目录: Attributes Boolean Attributes Style Attributes Class Attributes &Attributes Attributes jade中 ...

  7. Spring mvc框架 controller间跳转 ,重定向 ,传参

     一.需求背景     1. 需求:spring MVC框架controller间跳转,需重定向.有几种情况:不带参数跳转,带参数拼接url形式跳转,带参数不拼接参数跳转,页面也能显示.   @Req ...

  8. 烂泥:学习mysql的binlog配置

    本文由秀依林枫提供友情赞助,首发于烂泥行天下. 1.基础知识 日志是把数据库的每一个变化都记载到一个专用的文件里,这种文件就叫做日志文件.mysql默认只开启错误日志,因为过多的日志将会影响系统的处理 ...

  9. scp报错:not a regular file,解决方法:加参数 -r

    命令:scp  -P1234  /data/aa   root@192.0.0..0:/data 文件结构:/data/aa/yearmonth=2015-09 报错:not a regular fi ...

  10. Linux 下从头再走 GTK+-3.0 (二)

    仅仅创建一个空白窗口是不够的,下面我们为创建的窗口添加一个按钮. 以 Hello,World!为例. 首先创建一个源文件:example2.c 内容如下. #include <gtk/gtk.h ...