vue百度地图关键字检索定位 显示坐标到输入框 检索周边

原创 18932900560 随笔 前端 491阅读 2017-08-28 18:08:04 举报

mounted(){
// 百度地图API功能
var a = this;
function G(id) {
return document.getElementById(id);
}
var map = new BMap.Map("l-map");
map.centerAndZoom("石家庄", 11); // 初始化地图,设置城市和地图级别。
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;
    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<br />index = " + e.item.index + "<br />myValue = " + myValue;
a.record.name=myValue;
setPlace();
});

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

  var point = new BMap.Point(pp.lng,pp.lat);
  //搜索周边
    var local2 = new BMap.LocalSearch(map, { //医院
      onSearchComplete: function(results){
        var hospital = "";
        for (var i = 0; i < results.getCurrentNumPois(); i ++){
          hospital += results.getPoi(i).title + " ";
        }
        a.aroundConf.hospital=hospital;
      }
    });
    var local3 = new BMap.LocalSearch(map, { //地铁
      onSearchComplete: function(results){
        var metro = "";
        for (var i = 0; i < results.getCurrentNumPois(); i ++){
          metro += results.getPoi(i).title + "";
        }
      a.record.metro=metro;
      }
    });
    var local4 = new BMap.LocalSearch(map, { //商圈
      onSearchComplete: function(results){
        var business_circle = "";
        for (var i = 0; i < results.getCurrentNumPois(); i ++){
          business_circle +=results.getPoi(i).title + " ";
        }
        a.record.business_circle=business_circle;
      }
    });
评论 ( 0 )
最新评论
暂无评论

赶紧努力消灭 0 回复