// 引入SDK核心类
    var QQMapWX = require('@/common/qqmap.js');

    // 实例化API核心类
    var qqmapsdk = new QQMapWX({
        key: 'DIWAZ-OWFC2-MXZUQ-C3WFR-FOOIF-44FGG' // 必填
    });

    let selectCity = {
        data() {
            return {
                //地址选择器
                chooseCity: false,
                columns: [],
                columnData: [],
                lastIndex: 0,
                //地址选择器
            }
        },
        watch: {
            chooseCity(val) {
                if (!(val && !this.columnData.length)) return
                //初始化选择地址
                qqmapsdk.getCityList({
                    success: (province) => { //成功后的回调
                        this.columnData = province.result[0]
                        qqmapsdk.getDistrictByCityId({
                            id: province.result[0][0].id, //对应接口getCityList返回数据的Id,如:北京是'110000'
                            success: (res) => { //成功后的回调
                                this.columns = [
                                    province.result[0].map((val) => val.name),
                                    res.result[0].map((val) => val.name),
                                ]
                            },
                        });
                    },
                });
            },
        },

        methods: {
            changeHandler(e) {
                if (e.indexs[0] == this.lastIndex) return
                this.lastIndex = e.indexs[0]
                qqmapsdk.getDistrictByCityId({
                    id: this.columnData[e.indexs[0]].id, //对应接口getCityList返回数据的Id,如:北京是'110000'
                    success: (res) => { //成功后的回调
                        this.$set(this.columns, 1, res.result[0].map(val => val.name))
                    },
                    fail: function(error) {},
                });
            },
            getCityData() {

            }
        }
    }






    export {
        selectCity
    }
//导入上面的方法
import { selectCity } from
'@/common/mixinFunc.js'
//混入
mixins: [selectCity],
//页面添加元素
<u-picker :show="chooseCity" ref="uPicker" :columns="columns" @confirm="confirm" @cancel="chooseCity=false"
            @change="changeHandler"></u-picker>

 

 posted on 2022-11-24 09:48  laremehpe  阅读(415)  评论(0编辑  收藏  举报