huicanvie
huicanvie
  • 发布:2020-08-13 22:22
  • 更新:2023-05-22 09:59
  • 阅读:3503

关于是否支持map的customCallout

分类:uni-app

微信小程序原生的map支持自定义气泡customCallout,uni-app现在还不支持吗

2020-08-13 22:22 负责人:无 分享
已邀请:
七月羽歌

七月羽歌 - www.qiubg.com

支持啊。请看文档:

https://uniapp.dcloud.io/component/map?id=map

https://developers.weixin.qq.com/miniprogram/dev/component/map.html

可以这么来

  1. 定义 map 和数据。

            <map  
                id="map"  
                ref="map"  
                :show-location="true"  
                :scale="map.scale"  
                :markers="map.markers"  
                @regionchange="regionchange"  
                @updated="updated"  
                @markertap="touch"  
            >  
                <cover-view slot="callout">  
                    <cover-view v-for="(item, index) in map.callouts" :marker-id="item" class="callout-bg">  
                        <cover-view class="text-color-2 text-size-26 callout-label">{{map.list[item]['label']}}</cover-view>  
                        <cover-view class="text-color-2 text-size-26 callout-value">{{map.list[item]['value']}}</cover-view>  
                    </cover-view>  
                </cover-view>  
            </map>

    这里很重要的三个数据:map.markers、map.callouts、map.list。 map.markers 中定义 id,经纬度,customCallout这些。并设置宽高都为 0。而 customCallout 就那么几个值(display,anchorX,anchorY)。map.callouts 是个数组,就是 map.markers 中 id 组成的数组,map.list 是一个 object,其 key 就是上边的 id,值也是一个 object,比如区域,数字等。 就是你要使用 customCallou 的目的。自定义所要用到的数据就放在 map.list 里。

  2. 那么开始渲染真正的数据。

    data() {  
    return {  
     map: {  
    scale: 13,  
    markers: [],  
    callouts: [],  
    list: null  
     }  
    }  
    },  
    onLoad() {  
    this.initialize();  
    },  
    methods: {  
    initialize() {  
    let cityList = [  
                    {  
                        area: {  
                            label: '奉贤区',  
                            value: '310120'  
                        },  
                        count: 1,  
                        position: [30.91803, 121.4741],  
                        radius: 30  
                    },  
                    {  
                        area: {  
                            label: '杨浦区',  
                            value: '310110'  
                        },  
                        count: 1,  
                        position: [31.25956, 121.52609],  
                        radius: 30  
                    },  
                    {  
                        area: {  
                            label: '黄浦区',  
                            value: '310101'  
                        },  
                        count: 2,  
                        position: [31.23162, 121.48461],  
                        radius: 30  
                    }];  
    
            let markers = [];  
            let callouts = [];  
            let list = {};  
            for (let item of cityList) {  
                let marker = {  
                    id: parseInt(item.area.value),  
                    latitude: item.position[0],  
                    longitude: item.position[1],  
                    width: 0,  
                    height: 0,  
                    customCallout: {  
                        display: 'ALWAYS',  
                        anchorY: 0,  
                        anchorY: 40  
                    }  
                };  
                markers.push(marker);  
                callouts.push(marker.id);  
                list[marker.id] = {  
                    label: item.area.label,  
                    value: item.count  
                }  
            }  
            this.map.list = list;  
            this.map.callouts = callouts;  
            this.map.markers = markers;  
    
    }  
    }

再看 css 样式:

    .callout-bg {  
        display: flex;  
        flex-direction: column;  
        justify-content: center;  
        align-items: center;  
        background-color: #477ee8;    
        width: 80px;  
        height: 80px;  
        border-radius: 50%;  
        z-index: 9999;  

        .callout-label {  
            margin-bottom: 8px;  
        }  
    }
  • 1***@qq.com

    感谢老哥

    2021-08-10 21:32

  • 李yiyi

    为啥我这么写气泡出不来?

    2021-08-25 16:54

  • 咸鱼三月丶

    回复 李yiyi: cover -view 要写在map标签里面哦

    2021-08-27 11:22

  • 李yiyi

    回复 1***@qq.com: 是写在里面的

    2021-08-27 15:57

  • 李yiyi

    回复 1***@qq.com: 它需要再次点击才能显示出来,之后切换就都能出来了,就是首次出不来

    2021-08-27 15:58

  • 李yiyi

    回复 1***@qq.com: 我在cover-view里面用了cover-image,是图片出不来(唉

    2021-08-27 15:59

  • 小E

    回复 李yiyi: 点击两次才显示customCallout的问题,你解决了吗?

    2022-02-14 16:24

3***@qq.com

3***@qq.com - 1

同问 感觉是不支持

  • huicanvie (作者)

    嗯,文档中没有这个属性

    2020-08-15 10:30

  • 3***@qq.com

    回复 huicanvie: 原生小程序可以吗?真机测试我这直接固定在屏幕上了,没有跟随marker点,

    2020-08-15 13:04

  • huicanvie (作者)

    回复 3***@qq.com: 原生的v2.12.0版本以上都支持customCallout了。自己写的话,无法跟随marker点移动,这个就蛋疼了

    2020-08-16 00:20

暮雪骄阳

暮雪骄阳

晕死.....看到这个帖子是2020年的,心都凉了

  • FunnyKing

    这个map组件搞个自定义marker,结果搞的一头雾水,APP、H5、小程序不是驴不走就是磨不动!

    2021-03-30 03:13

  • 暮雪骄阳

    回复 FunnyKing: 搞不定除非自己接API了,不费劲了。一起祈祷吧。

    2021-03-30 03:38

ddpapa

ddpapa

感觉官方文档写的支持,到了我手里怎么都显示不出来,cover-view一堆问题

要回复问题请先登录注册