百度地图Cannot read property 'setStyle' of undefined

发布于 2022-09-11 17:08:14 字数 3748 浏览 22 评论 0

问题描述

使用点聚合 BmlMarkerClusterer + bm-marker + bm-label会报错,效果正常显示

图片描述

[Vue warn]: Error in callback for watcher "labelStyle": "TypeError: Cannot read property 'setStyle' of undefined"

found in

---> <BmLabel>
       <BmMarker>
         <BmlMarkerCluseter>
           <BmMap>
             <Map> at src/views/baiduMap/index.vue
               <AppMain> at src/views/layout/components/AppMain.vue
                 <Layout> at src/views/layout/Layout.vue
                   <App> at src/App.vue
                     <Root>

问题出现的环境背景及自己尝试过哪些方法

单独使用 bm-marker + bm-label不报错,效果正常显示

相关代码

<template>
  <baidu-map
    class="map"
    center="杭州"
    :scroll-wheel-zoom="true"
    :zoom='10'
  >
    <bm-city-list anchor="BMAP_ANCHOR_TOP_LEFT"></bm-city-list>
    <bm-geolocation
      anchor="BMAP_ANCHOR_BOTTOM_RIGHT"
      :showAddressBar="true"
      :autoLocation="true"
    ></bm-geolocation>
    <bml-marker-clusterer :averageCenter="true">
      <bm-marker
        v-for="(marker,index) of markers"
        :key="index"
        :position="{lng: marker.lng, lat: marker.lat}"
        @click='goInfo'
        :icon="{url: marker.icon , size: {width: 32, height: 32}}"
      >
        <bm-label
          :content="marker.info"
          :labelStyle="{color: marker.color , fontSize : '14px'}"
          :offset="{width: -44, height: 30}"
        />
      </bm-marker>
    </bml-marker-clusterer>

  </baidu-map>

</template>

<script>
import { BmlMarkerClusterer } from "vue-baidu-map";
export default {
  name: "Map",
  data() {
    return {
      markers: [
        {
          lng: 120.277888,
          lat: 30.289263,
          info: "120.277,30.289",
          color: "green",
          icon: "http://liangzhun.billyzc.com/img/green.png"
        },
        {
          lng: 120.390572,
          lat: 30.273794,
          info: "120.390,30.273",
          color: "red",
          icon: "http://liangzhun.billyzc.com/img/red.png"
        },
        {
          lng: 120.13071,
          lat: 30.187423,
          info: "120.130,30.187",
          color: "red",
          icon: "http://liangzhun.billyzc.com/img/red.png"
        },
        {
          lng: 120.489357,
          lat: 30.392492,
          info: "120.489,30.392",
          color: "red",
          icon: "http://liangzhun.billyzc.com/img/red.png"
        },
        {
          lng: 121.552173,
          lat: 31.344557,
          info: "120.489,30.392",
          color: "red",
          icon: "http://liangzhun.billyzc.com/img/red.png"
        },
        {
          lng: 121.497413,
          lat: 31.198628,
          info: "120.489,30.392",
          color: "red",
          icon: "http://liangzhun.billyzc.com/img/red.png"
        },
        {
          lng: 121.468667,
          lat: 31.114078,
          info: "120.489,30.392",
          color: "red",
          icon: "http://liangzhun.billyzc.com/img/red.png"
        }
      ]
    };
  },
  mounted() {
    // for (let i = 0; i < 10; i++) {
    //   const position = {lng: Math.random() * 40 + 85, lat: Math.random() * 30 + 21}
    //   this.markers.push(position)
    // }
  },
  methods: {
    goInfo(e) {
      console.log(e);
    }
  },
  components: {
    BmlMarkerClusterer
  }
};
</script>

<style lang='scss' scoped>
/* 百度地图必须设置宽高 */
.map {
  width: 100%;
  height: 600px;
}
</style>

你期待的结果是什么?实际看到的错误信息又是什么?

使用点聚合 BmlMarkerClusterer + bm-marker + bm-label效果正常显示
期望不报vue warn

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(1

再可℃爱ぅ一点好了 2022-09-18 17:08:14

data() {
return {

labelStyle: {color: marker.color , fontSize : '14px'}

}
}
<bm-label
:content="marker.info"
:labelStyle="labelStyle"
:offset="{width: -44, height: 30}"
/>
总结:labelStyle样式在data中定义为一个对象,然后在labelStyle使用labelStyle对象。

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文