{{ msg }}

当前位置:   article > 正文

Vue中使用百度地图demo Vue Baidu Map(vue-baidu-map)设置窗口信息_vue 百度地图自定义窗口

vue 百度地图自定义窗口

1.全局引入

安装  npm install --save vue-baidu-map

1.2全局配置 在main.js中配置

 

效果图:

二.代码

  1. <template>
  2. <div class="hello">
  3. <h1>{{ msg }}</h1>
  4. <!--百度地图-->
  5. <baidu-map
  6. class="bm-view"
  7. :center="center"
  8. :zoom="zoom"
  9. @ready="handler"
  10. :auto-resize="true"
  11. :scroll-wheel-zoom="true"
  12. >
  13. <bm-marker
  14. v-for="(item, index) in points"
  15. :key="index"
  16. :position="{ lng: item.lng, lat: item.lat }"
  17. @click="clickHandler(item.content,item.lng,item.lat)"
  18. :icon="item.icon"
  19. >
  20. </bm-marker>
  21. <bm-info-window
  22. :show="show"
  23. :position="windowposition"
  24. @close="infoWindowClose"
  25. @open="infoWindowOpen"
  26. >{{content}}</bm-info-window
  27. >
  28. <bm-polyline
  29. :path="points"
  30. stroke-style="dashed"
  31. stroke-color="blue"
  32. :stroke-opacity="0.3"
  33. :stroke-weight="1"
  34. ></bm-polyline>
  35. </baidu-map>
  36. </div>
  37. </template>
  38. <script>
  39. export default {
  40. name: 'HelloWorld',
  41. data () {
  42. return {
  43. msg: 'Welcome to map',
  44. center: { lng: 112, lat: 37 },
  45. zoom: 14,
  46. points: [],
  47. windowposition: {},
  48. content: {},
  49. show: false,
  50. icon: { url: "http://api.map.baidu.com/library/LuShu/1.2/examples/car.png"},
  51. }
  52. },
  53. mounted() {
  54. this.addPoints();
  55. },
  56. methods: {
  57. //地图实例
  58. handler({ BMap, map }) {
  59. console.log(BMap, map);
  60. this.center.lng = 112.404;
  61. this.center.lat = 37.755;
  62. this.zoom = 10;
  63. },
  64. clickHandler(content,lng,lat) {
  65. // alert(`单击点的坐标为:${e.point.lng}, ${e.point.lat}`);
  66. this.windowposition = { lng: lng, lat: lat };
  67. this.content=content;
  68. this.show = true;
  69. },
  70. addPoints() {
  71. const points = [];
  72. for (var i = 0; i < 10; i++) {
  73. let icon = this.icon;
  74. const position = {
  75. lng: Math.random() + 112,
  76. lat: Math.random() + 37,
  77. content: "我爱北京天安门" + i,
  78. ico: icon,
  79. };
  80. points.push(position);
  81. }
  82. this.points = points;
  83. console.log("this.points", this.points);
  84. },
  85. infoWindowOpen() {
  86. this.show = true;
  87. },
  88. infoWindowClose() {
  89. this.show = false;
  90. },
  91. }
  92. }
  93. </script>
  94. <style scoped>
  95. .bm-view {
  96. width: 100%;
  97. height: 800px;
  98. }
  99. </style>

3.参考百度Vue Baidu Map 

4.代码下载 代码下载 

声明:本文内容由网友自发贡献,不代表【wpsshop博客】立场,版权归原作者所有,本站不承担相应法律责任。如您发现有侵权的内容,请联系我们。转载请注明出处:https://www.wpsshop.cn/w/我家自动化/article/detail/109703
推荐阅读