vue-elementui中引入百度地图-索引提示

亲测可用

在vue主站index.html中引入百度地图:需要修改自己的ak

<script type="text/javascript" src="//api.map.baidu.com/api?v=2.0&ak=youak"></script>

案例:

准备输入地址页面

<el-form-item prop="address" label="地址">
    <!-- id必须设定并且需要双向绑定该地址 -->
    <el-input type="text" v-model="address" auto-complete="off" placeholder="请输入地址!" id="suggestId"></el-input>
</el-form-item>

在需要引入的vue页面中使用页面加载事件:mounted(){},具体引入代码如下

mounted() {
    //将当前this即为vue对象赋值给变量vue
    var vue = this;
    console.log(this);//这里还是vue的对象
    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<br />index = " + e.fromitem.index + "<br />value = " + value;
        value = "";
        if (e.toitem.index > -1) {
            _value = e.toitem.value;
            value = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        }
        str += "<br />ToItem<br />index = " + e.toitem.index + "<br />value = " + value;
    });
    var myValue;
    ac.addEventListener("onconfirm", function(e) {    //鼠标点击下拉列表后的事件
        var _value = e.item.value;
        myValue = _value.province +  _value.city +  _value.district +  _value.street +  _value.business;
        vue.shop.address = myValue;
        console.log(vue);
        console.log(this);//这里就不是vue对象了是map对象
        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);
    });
}

效果演示:

1、页面显示准备

 2、输入地址出现索引

猜你喜欢

转载自blog.csdn.net/qq_45987995/article/details/125361786