• vue2使用百度地图标记点弹出框添加背景图并且根据数据动态切换


    需求:vue2项目使用了百度地图,之后因为原始点标记的弹窗口样式太丑了,之后UI设计了框的样式图片后,替换原有的样式,本文章主要是更改样式和动态切换框的样式

    1.参考文档

    Vue-Baidu-Map 官方文档
    百度开放平台

    2.效果

    2.1 默认点弹框样式

    2.2 修改后的样式

    3.步骤

    3.1 下载百度地图插件

    npm install vue-baidu-map

    在main.js文件导入

    1. import BaiduMap from 'vue-baidu-map'
    2. Vue.use(BaiduMap, {
    3. // ak 是在百度地图开发者平台申请的密钥 详见 http://lbsyun.baidu.com/apiconsole/key */
    4. ak: ''
    5. })

    3.2 在页面导入注册使用

    center:地图中心点

    scroll-wheel-zoom:开启滚动缩放

    zoom:缩放级别

    @ready:渲染

    1. <BMap class="map" :center="center" :scroll-wheel-zoom="true" :zoom="zoom" @ready="bMapLoad" />
    2. import BMap from 'vue-baidu-map/components/map/Map';
    3. components: {
    4. BMap
    5. },

    3.3 隐藏百度地图原有左下角logo

    1. :deep() {
    2. .BMap_cpyCtrl {
    3. display: none;
    4. }
    5. .anchorBL {
    6. display: none;
    7. }}

    3.4 隐藏原有弹窗样式

    sContentStyle:窗体的大小,一定要设置,如果设置过小,之后的图片都放不上去

    1. // 窗体样式
    2. var sContentStyle = {
    3. width: 430,
    4. height: 260
    5. };
    6. var infoWindow = new mapConstructor.InfoWindow(sContent, sContentStyle);
    7. marker.addEventListener('click', function (e) {
    8. this.openInfoWindow(infoWindow);
    9. });

    窗体先把样式全部设置为不显示

    1. .BMap_pop img,
    2. .BMap_top,
    3. .BMap_center,
    4. .BMap_bottom,
    5. .BMap_pop > div {
    6. &:not(:nth-child(9)) {
    7. display: none;
    8. }
    9. }

    3.5添加背景图片并且动态切换

    我这边是通过是否在线的状态来切换窗体样式

    1. // 窗体内容
    2. let sContent = `
      : 'warningInfo'}'>

    3. 地址: ${element.address}
      • 经度:${element.longitude}
  • `;
  • 先给这俩个设置统一的大小,注意,不能超过sContentStyle的大小,不然会被隐藏!!!

    1. .infowindow,
    2. .warningInfo {
    3. height: 245px !important;
    4. width: 100% !important;
    5. box-sizing: border-box;
    6. padding: 0px 20px;
    7. font-size: 13px;
    8. .info-main {
    9. display: flex;
    10. ul {
    11. flex: 1;
    12. li {
    13. line-height: 29px;
    14. }
    15. }
    16. ul:nth-child(1) {
    17. flex: 0 0 180px;
    18. }
    19. }
    20. }

    之后给每个单独设置不同的背景图片

    1. .infowindow {
    2. background: url('../../../assets/img/onlineInfo.png') no-repeat;
    3. background-size: 100% 100%;
    4. }
    5. .warningInfo {
    6. background: url('../../../assets/img/offlineInfo.png') no-repeat;
    7. background-size: 100% 100%;
    8. .infowindow-title {
    9. color: #ffdb4c;
    10. }
    11. }

    3.6 标记点动态

    一样的,也是根据online来判断之后添加到地图上

    1. let myIcon;
    2. const mPoint = new mapConstructor.Point(element.longitude, element.latitude);
    3. if (element.online) {
    4. myIcon = new mapConstructor.Icon(
    5. require('@/assets/img/online.png'), // 在线图标
    6. new mapConstructor.Size(80, 114) //宽,高
    7. );
    8. } else {
    9. myIcon = new mapConstructor.Icon(
    10. require('@/assets/img/offline.png'), // 离线图标
    11. new mapConstructor.Size(70, 119)
    12. );
    13. }
    14. var marker = new mapConstructor.Marker(mPoint, {
    15. icon: myIcon //注意icon i小写,负责图标无法显示
    16. // enableDragging: true, // 拖拽
    17. });
    18. // marker.setAnimation(BMAP_ANIMATION_BOUNCE);
    19. this.map.addOverlay(marker);

    4.完整代码

    1. <script>
    2. import BMap from 'vue-baidu-map/components/map/Map';
    3. import { mapMutations, mapState } from 'vuex';
    4. let mapConstructor; // 百度地图构造函数存放容器
    5. export default {
    6. components: {
    7. BMap
    8. },
    9. data() {
    10. return {
    11. center: {
    12. lng: '116.46',
    13. lat: '39.92'
    14. },
    15. zoom: 8,
    16. map: null, // 地图容器
    17. BMap: null, // 构造器容器
    18. pointsList: [
    19. {
    20. longitude: '116.46',
    21. latitude: '39.92',
    22. address: '北京',
    23. online: 1
    24. },
    25. {
    26. longitude: '112.46',
    27. latitude: '39.92',
    28. address: '呼和浩特',
    29. online: 0
    30. }
    31. ]
    32. };
    33. },
    34. mounted() {},
    35. methods: {
    36. // BMap加载触发实例化方法
    37. bMapLoad({ BMap, map }) {
    38. this.$nextTick(() => {
    39. // 百度地图容器全局化
    40. this.map = map;
    41. // 百度地图构造函数全局化
    42. mapConstructor = BMap;
    43. this.BMap = BMap;
    44. // 清除地图上的覆盖物
    45. this.map.clearOverlays();
    46. // this.map.enableScrollWheelZoom(true);
    47. this.map.getPanes().floatShadow.style.display = 'none'; // 清除阴影
    48. // this.map.setMapType(BMAP_SATELLITE_MAP); //卫星地图
    49. this.map.setMapType(BMAP_HYBRID_MAP); //卫星混合地图
    50. this.bMapPoint();
    51. // this.getBase();
    52. });
    53. },
    54. // 在百度地图上打点方法
    55. bMapPoint() {
    56. // 清空标记点
    57. this.map.clearOverlays();
    58. // 对多个经纬度点进行标注
    59. // console.log(this.deviceList, "获取到的数据-----------");
    60. // 设置地图中心点
    61. // this.map.centerAndZoom(this.BMap.Point(117.0, 36.4), 12);
    62. // console.log(this.deviceList, "获取到了设备的列表数据了没");
    63. this.pointsList.forEach((element) => {
    64. // var myIcon = new mapConstructor.Icon( // 自定义图标
    65. // require("@/assets/img/icon1.png"),
    66. // new mapConstructor.Size(80, 104) // 图标的宽度和高度
    67. // );
    68. // 定义两个图标,一个表示在线,一个表示离线
    69. let myIcon;
    70. const mPoint = new mapConstructor.Point(element.longitude, element.latitude);
    71. if (element.online) {
    72. myIcon = new mapConstructor.Icon(
    73. require('@/assets/img/online.png'), // 在线图标
    74. new mapConstructor.Size(80, 114) //宽,高
    75. );
    76. } else {
    77. myIcon = new mapConstructor.Icon(
    78. require('@/assets/img/offline.png'), // 离线图标
    79. new mapConstructor.Size(70, 119)
    80. );
    81. }
    82. var marker = new mapConstructor.Marker(mPoint, {
    83. icon: myIcon //注意icon i小写,负责图标无法显示
    84. // enableDragging: true, // 拖拽
    85. });
    86. // marker.setAnimation(BMAP_ANIMATION_BOUNCE);
    87. this.map.addOverlay(marker);
    88. // 窗体内容
    89. let sContent = `
      : 'warningInfo'}'>

    90. 地址: ${element.address}
      • 经度:${element.longitude}
      • `;
      • // 窗体样式
      • var sContentStyle = {
      • width: 430,
      • height: 260
      • };
      • var infoWindow = new mapConstructor.InfoWindow(sContent, sContentStyle);
      • marker.addEventListener('click', function (e) {
      • this.openInfoWindow(infoWindow);
      • });
      • });
      • }
      • }
      • };
      • script>
      • <style lang="scss" scoped>
      • .map {
      • width: 100%;
      • // height: calc(100vh - 240px);
      • height: 100%;
      • }
      • :deep() {
      • .BMap_cpyCtrl {
      • display: none;
      • }
      • .anchorBL {
      • display: none;
      • }
      • .infowindow,
      • .warningInfo {
      • height: 245px !important;
      • width: 100% !important;
      • box-sizing: border-box;
      • padding: 0px 20px;
      • font-size: 13px;
      • // p {
      • // line-height: 15px;
      • // }
      • .info-main {
      • display: flex;
      • ul {
      • flex: 1;
      • li {
      • line-height: 29px;
      • }
      • }
      • ul:nth-child(1) {
      • flex: 0 0 180px;
      • }
      • }
      • }
      • .infowindow {
      • background: url('../../../assets/img/onlineInfo.png') no-repeat;
      • background-size: 100% 100%;
      • }
      • .warningInfo {
      • background: url('../../../assets/img/offlineInfo.png') no-repeat;
      • background-size: 100% 100%;
      • .infowindow-title {
      • color: #ffdb4c;
      • }
      • }
      • .BMap_bubble_content {
      • height: 245px !important;
      • width: 430px !important;
      • color: #ffffff;
      • font-size: 14;
      • }
      • .BMap_pop img,
      • .BMap_top,
      • .BMap_center,
      • .BMap_bottom,
      • .BMap_pop > div {
      • &:not(:nth-child(9)) {
      • display: none;
      • }
      • }
      • }
      • style>

      文章到此结束,希望对你有所帮助~

    91. 相关阅读:
      百度小程序模板制作_百度小程序模板平台
      Safe Head机制技术实践分析
      alphapose 配置运行 win11
      websocket逆向
      c++——内存管理
      搜索留痕推广引流软件的作用#川圣SEO#蜘蛛池
      Drag-MoveMent
      【数据结构】纯c语言双向链表
      c++内存管理
      【计算机网络】HTTPS的基础知识
    92. 原文地址:https://blog.csdn.net/qq_44278289/article/details/140443889