• vue3使用腾讯地图选择地点


    在这里插入图片描述

    <template>
        <div>
            <el-dialog width="60%" :before-close="cancel" v-model="props.visible">
                <span>
                    <el-autocomplete v-model="addressKeyword" placeholder="请输入地址,例如临沂人民广场" clearable
                        style="margin-bottom:20px;width:100%;" :fetch-suggestions="querySearch" @select="handleSelect">
                        <template #append>
                            <el-button icon="el-icon-search" @click="getAddressKeyword">el-button>
                        template>
                    el-autocomplete>
                    <div id="container" style="width:100%;height:600px;">div>
                span>
                <template #footer>
                    <span class="dialog-footer">
                        <el-button @click="cancel">取 消el-button>
                        <el-button type="primary" @click="confirm">确 定el-button>
                    span>
                template>
            el-dialog>
        div>
    template>
      
    <script setup>
    import { ref, reactive, onMounted, nextTick } from "vue";
    import md5 from 'js-md5';
    import { jsonp } from 'vue-jsonp'
    
    const markersArray = ref([]);
    const restaurants = ref([]);
    const map = ref(null);
    const getAddress = ref(null);
    const getAddCode = ref(null);
    const addressKeyword = ref('');
    const shopInfo = reactive({
        lng: '',
        lat: '',
        address: ''
    });
    
    const props = defineProps({
        visible: Boolean,
        lng: String,
        lat: String
    })
    
    onMounted(() => {
        nextTick(() => {
            init()
        })
    })
    var markerLayer
    // 初始化地图
    const init = () => {
        // 定义地图中心点坐标
        var center = new TMap.LatLng(35.05151, 118.34787)
        // 定义map变量,调用 TMap.Map() 构造函数创建地图
        map.value = new TMap.Map(document.getElementById('container'), {
            center: center, // 设置地图中心点坐标
            zoom: 13, // 设置地图缩放级别
            pitch: 0, // 设置俯仰角
            rotation: 0 // 设置地图旋转角度
        })
        // 获取点击后的地址
        map.value.on('click', function (event) {
            shopInfo.address = event.poi.name
            // 获取点击后的地图坐标并设置Marker
            shopInfo.lng = event.latLng.lng
            shopInfo.lat = event.latLng.lat
            if (markerLayer) {
                markerLayer.setGeometries([])
            }
            markerLayer = new TMap.MultiMarker({
                map: map.value,
                styles: {
                    // 点标记样式
                    marker: new TMap.MarkerStyle({
                        width: 20, // 样式宽
                        height: 30, // 样式高
                        anchor: { x: 10, y: 30 }, // 描点位置
                        src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png' // 标记路径
                    })
                }
            })
            markerLayer.add({
                id: shopInfo.lat,
                position: new TMap.LatLng(shopInfo.lat, shopInfo.lng)
            })
        })
        if (props.lat && props.lng) {
            if (markerLayer) {
                markerLayer.setGeometries([])
            }
            markerLayer = new TMap.MultiMarker({
                map: map.value,
                styles: {
                    // 点标记样式
                    marker: new TMap.MarkerStyle({
                        width: 20, // 样式宽
                        height: 30, // 样式高
                        anchor: { x: 10, y: 30 }, // 描点位置
                        src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png' // 标记路径
                    })
                }
            })
            markerLayer.updateGeometries({
                styleId: "marker",
                id: props.lat,
                position: new TMap.LatLng(props.lat, props.lng)
            })
            map.value.setCenter(new TMap.LatLng(props.lat, props.lng))
        }
    }
    const querySearch = (queryString, cb) => {
        var results = queryString ? restaurants.value.filter(createFilter(queryString)) : restaurants.value
        // 调用 callback 返回建议列表的数据
        cb(results)
    }
    const createFilter = (queryString) => {
        return (restaurant) => {
            return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0)
        }
    }
    const handleSelect = (item) => {
        // 腾讯地图文档: https://lbs.qq.com/faq/serverFaq/webServiceKey
        // csdn参考:https://blog.csdn.net/tiankonlan/article/details/126363935
        let str = `/ws/geocoder/v1/?address=${item || addressKeyword.value}&callback=jsonpCallback&key=${import.meta.env.VITE_APP_TMapKey}&output=jsonp${import.meta.env.VITE_APP_TMapSig}`
        let sig = md5(str);
        sig = encodeURI(sig)
    
        let getData = {
            address: item || addressKeyword.value,
            callbackQuery: 'callback',
            callbackName: 'jsonpCallback',
            output: 'jsonp',
            key: import.meta.env.VITE_APP_TMapKey,
            sig: sig
        }
    
        jsonp('https://apis.map.qq.com/ws/geocoder/v1/', getData).then(response => {
            if (response.message === '查询无结果') {
                alert('查询无结果')
                return false
            }
            shopInfo.lng = response.result.location.lng
            shopInfo.lat = response.result.location.lat
            shopInfo.address = item
            if (markerLayer) {
                markerLayer.setGeometries([])
            }
            markerLayer = new TMap.MultiMarker({
                map: map.value,
                styles: {
                    // 点标记样式
                    marker: new TMap.MarkerStyle({
                        width: 20, // 样式宽
                        height: 30, // 样式高
                        anchor: { x: 10, y: 30 }, // 描点位置
                        src: 'https://mapapi.qq.com/web/lbs/javascriptGL/demo/img/markerDefault.png' // 标记路径
                    })
                }
            })
            map.value.setCenter(new TMap.LatLng(shopInfo.lat, shopInfo.lng))
            markerLayer.updateGeometries({
                id: shopInfo.lat,
                position: new TMap.LatLng(shopInfo.lat, shopInfo.lng)
            })
        }).catch(function (error) {
            console.log(error)
        })
    }
    const getAddressKeyword = () => {
        handleSelect(addressKeyword.value)
    }
    const emit = defineEmits(['map-confirm', 'cancel'])
    /***
     * 确认
     */
    const confirm = () => {
        let data = JSON.parse(JSON.stringify(shopInfo))
        emit('map-confirm', data)
    }
    /***
     * 取消
     */
    const cancel = () => {
        emit('cancel')
    }
    script>
      
    <style lang="scss" scoped>
    .serachinput {
        width: 300px;
        box-sizing: border-box;
        padding: 9px;
        border: 1px solid #dddee1;
        line-height: 20px;
        font-size: 16px;
        height: 38px;
        color: #333;
        position: relative;
        border-radius: 4px;
        -webkit-box-shadow: #666 0px 0px 10px;
        -moz-box-shadow: #666 0px 0px 10px;
        box-shadow: #666 0px 0px 10px;
    }
    
    :deep(.el-dialog__header) {
        border-bottom: 0 !important;
    }
    style>
      
      
    
    • 1
    • 2
    • 3
    • 4
    • 5
    • 6
    • 7
    • 8
    • 9
    • 10
    • 11
    • 12
    • 13
    • 14
    • 15
    • 16
    • 17
    • 18
    • 19
    • 20
    • 21
    • 22
    • 23
    • 24
    • 25
    • 26
    • 27
    • 28
    • 29
    • 30
    • 31
    • 32
    • 33
    • 34
    • 35
    • 36
    • 37
    • 38
    • 39
    • 40
    • 41
    • 42
    • 43
    • 44
    • 45
    • 46
    • 47
    • 48
    • 49
    • 50
    • 51
    • 52
    • 53
    • 54
    • 55
    • 56
    • 57
    • 58
    • 59
    • 60
    • 61
    • 62
    • 63
    • 64
    • 65
    • 66
    • 67
    • 68
    • 69
    • 70
    • 71
    • 72
    • 73
    • 74
    • 75
    • 76
    • 77
    • 78
    • 79
    • 80
    • 81
    • 82
    • 83
    • 84
    • 85
    • 86
    • 87
    • 88
    • 89
    • 90
    • 91
    • 92
    • 93
    • 94
    • 95
    • 96
    • 97
    • 98
    • 99
    • 100
    • 101
    • 102
    • 103
    • 104
    • 105
    • 106
    • 107
    • 108
    • 109
    • 110
    • 111
    • 112
    • 113
    • 114
    • 115
    • 116
    • 117
    • 118
    • 119
    • 120
    • 121
    • 122
    • 123
    • 124
    • 125
    • 126
    • 127
    • 128
    • 129
    • 130
    • 131
    • 132
    • 133
    • 134
    • 135
    • 136
    • 137
    • 138
    • 139
    • 140
    • 141
    • 142
    • 143
    • 144
    • 145
    • 146
    • 147
    • 148
    • 149
    • 150
    • 151
    • 152
    • 153
    • 154
    • 155
    • 156
    • 157
    • 158
    • 159
    • 160
    • 161
    • 162
    • 163
    • 164
    • 165
    • 166
    • 167
    • 168
    • 169
    • 170
    • 171
    • 172
    • 173
    • 174
    • 175
    • 176
    • 177
    • 178
    • 179
    • 180
    • 181
    • 182
    • 183
    • 184
    • 185
    • 186
    • 187
    • 188
    • 189
    • 190
    • 191
    • 192
    • 193
    • 194
    • 195
    • 196
    • 197
    • 198
    • 199
    • 200
    • 201
    • 202
    • 203
    • 204
    • 205
    • 206
    • 207
    • 208
    • 209
    • 210
    • 211
    • 212
  • 相关阅读:
    05-Spring Boot工程中简化开发的方式Lombok和dev-tools
    当发布/订阅模式遇上.NET
    整车热管理「升温」,哪些厂商排名电子风扇市场份额TOP10
    rapidocr-onnxruntime库及在open-webui上传PDF 图像处理 (使用 OCR)应用
    什么是VHDL?一文带你了解VHDL语言
    MySQL表的增删查改
    《图解HTTP》Chapter 1-4
    什么是访问控制漏洞
    数字图像处理(入门篇)目录
    【单片机入门】(三)应用层软件开发的单片机学习之路-----UART串口通讯和c#交互
  • 原文地址:https://blog.csdn.net/qq_40476712/article/details/133775035