1***@qq.com
1***@qq.com
  • 发布:2024-01-10 17:51
  • 更新:2024-04-29 15:21
  • 阅读:328

动态添加addMarkers h5不显示,小程序显示,customCallout为点击显示小程序点击无效,只能弄常显

分类:uni-app
<template>  
    <view class="content">  
        <map id="map" class="map" :style="{width:'100%',height:mapHeight+'px'}" ref="map" :show-location="true"  
            :latitude="data.latitude" :longitude="data.longitude" scale="10">  
            <cover-view slot="callout">  

                <cover-view v-for="(item,index) in data.positions" class="customCallout" :marker-id="item.id">  
                    <cover-view class="content">  
                        {{item}}-{{index}}  
                    </cover-view>  
                </cover-view>  

            </cover-view>  
        </map>  

        <view class="btn" ref='dome' id="btn">  
            <view class="btn-main">  
                成都市  
            </view>  
            <view class="btn-main">  

                更多地区  
                <br>  
                开发中…  
            </view>  
        </view>  
    </view>  
</template>  

<script lang="ts" setup>  
    import { onReady, onShow } from '@dcloudio/uni-app'  
    import { ref, reactive, getCurrentInstance } from 'vue'  

    const map = ref()  
    const dome = ref()  
    const mapHeight = ref()  
    const instance = getCurrentInstance();  
    let _mapContext : UniApp.MapContext;  
    const data = reactive({  
        latitude: 30.646291657333,  
        longitude: 104.0481414546,  
        positions: [  
            {  
                id: 1,  
                latitude: 30.646291657333,  
                longitude: 104.0481414546,  
            }, {  
                id: 2,  
                latitude: 30.660144317128,  
                longitude: 104.02842579404,  
            }  
        ],  
        mskers: [  
            {  
                id: 1,  
                latitude: 30.646291657333,  

                iconPath: '/static/location.png',  
                longitude: 104.0481414546,  
                width: 50,  
                height: 50,  
                customCallout: {  
                    anchorY: 0,  
                    anchorX: 0,  
                    //display: 'BYCLICK'  
                    //display: 'ALWAYS'  
                },  
            }, {  
                id: 2,  
                latitude: 30.660144317128,  
                width: 50,  
                height: 50,  
                iconPath: '/static/location.png',  
                longitude: 104.02842579404,  
                customCallout: {  
                    anchorY: 0,  
                    anchorX: 0,  
                    //display: 'BYCLICK'  
                    //display: 'ALWAYS'  
                },  
            }  
        ]  
    })  

    onReady(() => {  
        const { windowHeight } = uni.getSystemInfoSync()  

        _mapContext = uni.createMapContext("map");  
        const query = uni.createSelectorQuery().in(instance?.ctx);  
        query.select('#btn').boundingClientRect(data => {  
            mapHeight.value = (windowHeight - data.height)  
        }).exec();  

        _mapContext.initMarkerCluster({  
                enableDefaultStyle: false,  
                zoomOnClick: true,  
                gridSize: 60,  
                complete(res) {  
                  console.log('initMarkerCluster', res)  
                }  
              });  

              _mapContext.on("markerClusterCreate", (e) => {  
                console.log("markerClusterCreate", e);  
              });  
        addMarkers(_mapContext)  
    })  

    const addMarkers = (maskesFn : UniApp.MapContext) => {  

        const markers = []  

        data.positions.forEach((p, i) => {  

            markers.push(  
                Object.assign({}, {  

                    iconPath: '/static/location.png',  
                    // width: 50,  
                    // height: 50,  
                    // label: {  
                    //  width: 50,  
                    //  height: 30,  
                    //  borderWidth: 1,  
                    //  borderRadius: 10,  
                    //  bgColor: '#fff',  
                    //  content: `label ${i + 1}`  
                    // },  
                    width: 50,  
                    height: 50,  

                    customCallout: {  
                        anchorY: 0,  
                        anchorX: 0,  
                        display: 'BYCLICK'  
                        //display: 'ALWAYS'  
                    },  

                }, p)  
            )  
        })  

        try {  
            // ...  
            console.log('Inside addMarkers');  

            maskesFn.addMarkers({  
                markers,  
                clear: false,  
                complete(res) {  
                    console.log('addMarkers complete', res);  
                },  
                fail(res) {  
                    console.log('addMarkers fail', res);  
                }  
            });  
            // ...  
        } catch (error) {  
            console.log('Error in addMarkers', error);  
        }  
    }  
</script>  

<style lang="scss" scoped>  
    /*  uni.getLocation({  
            type: 'gcj02',  
            success: function (res) {  

                console.log('当前位置的经度:' + res.longitude);  
                console.log('当前位置的纬度:' + res.latitude);  
                data.latitude = res.latitude;  
                data.longitude = res.longitude;  

            /*  uni.openLocation({  
                    latitude: latitude,  
                    longitude: longitude,  
                    success: function () {  
                        console.log('success');  
                    }  
                }); */  
    /*   uni.chooseLocation({  
                    success: function (res) {  
                        console.log('位置名称:' + res.name);  
                        console.log('详细地址:' + res.address);  
                        console.log('纬度:' + res.latitude);  
                        console.log('经度:' + res.longitude);  
                    }  
                 });   

            },  
            fail: (r) => {  
                console.log(r)  
            },  
            complete: (r) => {  
                console.log(r)  
            }  
        });  
         */  
    .content {  
        flex: 1;  
    }  

    .map {  
        flex: 1;  
        /* height: 100vh; */  
        width: 100vh;  
    }  

    .btn {  
        width: 100%;  
        padding: 15px;  
        display: flex;  
        gap: 10px;  

        .btn-main {  
            background-color: #ededed;  
            width: 100px;  
            height: 70px;  
            display: flex;  
            justify-content: center;  
            align-items: center;  
            flex-direction: column;  
        }  
    }  

    .customCallout {  
        box-sizing: border-box;  
        background-color: #fff;  
        border: 1px solid #ccc;  
        border-radius: 30px;  
        // width: 150px;  
        // height: 40px;  
        display: inline-flex;  
        padding: 5px 20px;  
        justify-content: center;  
        align-items: center;  
    }  
</style>
2024-01-10 17:51 负责人:无 分享
已邀请:
iOSDeveloper

iOSDeveloper - 专注于iOS相关领域

可以提供一下示例工程

DCloud_UNI_OttoJi

DCloud_UNI_OttoJi - 日常回复 uni-app/x 问题,如果艾特我没看到,请主动私信

看了好久,明白你的意思了,有些代码展示用不到。

  1. 小程序支持动态 addMarkers ,h5 不支持,已复现
  2. customCallout 不能点击展示。这个我测试可以,我贴一段代码,你看看。当你点击图标时候,会展示一个自定义的气泡。安卓真机调试可以运行
<template>  
  <div class="w-full">  
    <map  
      :longitude="dot.longitude"  
      :latitude="dot.latitude"  
      show-location  
      :markers="markers"  
      style="outline: 1px solid red; width: 746rpx; height: 50vh"  
    >  
      <cover-view slot="callout">  
        <cover-view class="customCallout" :marker-id="11">  
          <cover-view class="content">  
            <cover-view style="background-color: blue; color: red">666</cover-view>  
          </cover-view>  
        </cover-view>  
      </cover-view>  
    </map>  
  </div>  
</template>  
<script lang="ts" setup>  
import { onUnmounted } from "vue";  

const dot = { latitude: 39.916633014861084, longitude: 116.3198844199444 };  
const dot2 = {  
  latitude: 39.919998434612495,  
  longitude: 116.31794540154829,  
};  

// markers 显示标记  
const markers = [  
  {  
    id: 11,  
    longitude: dot.longitude,  
    latitude: dot.latitude,  
    // title: "标注点名",  
    iconPath: "/static/logo.png",  
    rotate: 0, //45,  
    // alpha: 0.5,  
    width: 50,  
    height: 50,  
    anchor: {  
      x: 0.5, // 0-1  
      y: 0.5,  
    },  
    // callout: {  
    //   content: "文本 11",  
    //   color: "#ff0000",  
    //   fontSize: 16,  
    //   borderRadius: 10,  
    //   borderWidth: 2,  
    //   borderColor: "#000000",  
    //   bgColor: "#ffffff",  
    //   padding: 5,  
    //   display: "BYCLICK",  
    //   // display: "ALWAYS", // BYCLICK  
    //   textAlign: "center",  
    //   anchorX: 0,  
    //   anchorY: 0,  
    // },  
    // label: {  
    //   content: "label 文本",  
    //   color: "#ff0000",  
    //   fontSize: 16,  
    //   // x,y  
    //   borderRadius: 10,  
    //   borderWidth: 2,  
    //   borderColor: "#000000",  
    //   bgColor: "#ffffff",  
    //   padding: 5,  
    //   textAlign: "center",  
    //   ["aria-label"]: "标注地名23",  
    // },  
    // anchor: {},  
    // clusterId:1,  
    customCallout: {  
      display: "BYCLICK",  
    },  
    ["aria-label"]: "标注地名",  
    // joinCluster:true  
  },  
];  

onUnmounted(() => {  
  console.log("map onUnmounted");  
});  
</script>  
1***@163.com

1***@163.com

addMarkers动态增加h5真的不触发现在解决了 吗
console.log(marker,'marker');
// map.value.addMarkers({
// markers: marker,
// clear:false,
// success:function(res){
// console.log(res,'成功');

        //     },  
        //     fail:function(err){  
        //       console.log('err',err )  
        //     },  
        //     complete:function(err){  
        //         console.log(err,'完成');  

        //     }  
        // }),这个不生效

要回复问题请先登录注册