vue调用百度地图API输入提示触发下拉列表的问题

2019-06-17 11:32:19长沙做网站严微

解决问题后的界面效果图:

在开发中,获取百度地图API,输入关键词时触发下拉列表时所遇到的问题:看下图


用的示例是:http://lbsyun.baidu.com/jsdemo.htm#a6_2

// 百度地图API功能
	function G(id) {
		return document.getElementById(id);
	}

	var map = new BMap.Map("l-map");
	map.centerAndZoom("北京",12);                   // 初始化地图,设置城市和地图级别。

	var ac = new BMap.Autocomplete(    //建立一个自动完成的对象
		{"input" : "suggestId"
		,"location" : map
	});

	ac.addEventListener("onhighlight", function(e) {  //鼠标放在下拉列表上的事件
	var str = "";
		var _value = e.fromitem.value;
		var value = "";
		if (e.fromitem.index > -1) {
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str = "FromItem
index = " + e.fromitem.index + "
value = " + value;
		
		value = "";
		if (e.toitem.index > -1) {
			_value = e.toitem.value;
			value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		}    
		str += "
ToItem
index = " + e.toitem.index + "
value = " + value;
		G("searchResultPanel").innerHTML = str;
	});

	var myValue;
	ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
	var _value = e.item.value;
		myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
		G("searchResultPanel").innerHTML ="onconfirm
index = " + e.item.index + "
myValue = " + myValue;
		
		setPlace();
	});

	function setPlace(){
		map.clearOverlays();    //清除地图上所有覆盖物
		function myFun(){
			var pp = local.getResults().getPoi(0).point;    //获取第一个智能搜索的结果
			map.centerAndZoom(pp, 18);
			map.addOverlay(new BMap.Marker(pp));    //添加标注
		}
		var local = new BMap.LocalSearch(map, { //智能搜索
		  onSearchComplete: myFun
		});
		local.search(myValue);
	}

下面给出自己解决的方法:

html代码:

<el-autocomplete
        class="inline-input"
        v-model="addressDetail"
        :fetch-suggestions="querySearch"
        placeholder="搜索您的街道、小区、大厦名称"
        :trigger-on-focus="false"
        :maxlength="20"
        @select="handleSelect">
        <template slot-scope="{ item }">
            {{ item.address }}<span>{{item.city}}</span>
        </template>
</el-autocomplete>

JS代码:

export default {
        name: 'recover-page',
        data () {
            return {
                addressDetail : null,
                adderss : '',

                map: null,
                local: null,
                restaurants: []
            }
        },
        props: {},
        watch: {
            addressDetail(val) {//监听addressDetail的变化
                this.local.search(val);
            }
        },
        computed: {},
        methods: {
            
            querySearch(queryString, cb) {
                let restaurants = [];
                restaurants = this.restaurants;
                let results = queryString ? restaurants.filter(this.createFilter(queryString)) : restaurants;
                // 调用 callback 返回建议列表的数据
                cb(results);
            },
            createFilter(queryString) {
                /*return (restaurant) => {
                    return (restaurant.value.toLowerCase().indexOf(queryString.toLowerCase()) === 0);
                };*/
                return (restaurant) => {
                    return restaurant
                }
            },
            loadAll() {
                return this.restaurants;
            },
            handleSelect(item) {
                console.log("item",item);
                this.map.clearOverlays();
                this.addressDetail = item.address;
                const pp = item.point;
                this.map.centerAndZoom(pp, 15);
                this.map.addOverlay(new BMap.Marker(pp));
            },
            baiduMap : function () {
                //let map = new BMap.Map("allmap");
                let _this = this;
                _this.map.centerAndZoom("广州",12); // 初始化地图,设置城市和地图级别。
                let geolocation =new BMap.Geolocation();
                geolocation.getCurrentPosition(function(r){
                    if (this.getStatus()==BMAP_STATUS_SUCCESS) {
                        let mk=new BMap.Marker(r.point);
                        _this.map.addOverlay(mk);
                        _this.map.panTo(r.point);
                        //alert('您的位置:'+r.point.lng+','+r.point.lat);
                    }else {
                        //alert('您的手机不支持定位服务');
                    }
                });
                _this.local = new BMap.LocalSearch(_this.map, {
                    onSearchComplete: function(results){
                        // 判断状态是否正确
                        if (_this.local.getStatus() == BMAP_STATUS_SUCCESS){
                            _this.restaurants = [];
                            for (let i = 0; i < results.getCurrentNumPois(); i ++){
                                _this.restaurants.push(results.getPoi(i));
                            }
                        }
                    }
                });
                //_this.local.search(_this.addressDetail);
            },

        },
        components: {
        },
        beforeCreate() {
        },
        created() {
        },
        mounted() {
            this.map = new BMap.Map("allmap");
            this.$nextTick(()=>{
                let t1 = setTimeout(()=>{
                    this.baiduMap();
                    clearTimeout(t1)
                },10);
            });
            this.loadAll();

        },
        beforeMount() {
        },
        beforeUpdate() {
        },
        updated() {
        },
        activated() {
        },
        deactivated() {
        },
        beforeDestroy() {
        },
        destroyed() {
        },
        errorCaptured() {
        }
    }

嗯,问题解决!

上一篇:

HTML5-语义化

下一篇:

vue.js打包之后可能会遇到的坑! 返 回

Copyright © 2009-2019 Ywcms.Com All Right Reserved.  湖南严微网络科技有限公司©版权所有   湘ICP备16006489号-2