高德搜索定位,获取信息

实现一个带搜索功能的地图,并且获取选取的地点信息,下面我们开始吧

效果图:
效果图
代码:

<script>
export default {
name: 'map-show',
  data () {
    return {
      map: null,
    dataRes: {}
    }
  },
  mounted () {
    this.initMap()
  },
  methods: {
    // 实例化地图
    initMap () {
      const AMap = window.AMap
      this.map = new AMap.Map('container', {
        resizeEnable: true,
     zoom: 12,
     center: [120.204388,30.244759]
      })
    },// 输出数据
    output(name) {
    // name为字符串,'北京'、'上海'、'杭州'
      // 位置联想搜索,并标记
      let that = this
      const AMap = window.AMap
      AMap.plugin(['AMap.Autocomplete', 'AMap.PlaceSearch'],function(){
        let autoOptions = {
          city: name, // 城市,默认全国
          input: 'keyword' // 使用联想输入的input的id
        }
        let autocomplete= new AMap.Autocomplete(autoOptions)
        const placeSearch = new AMap.PlaceSearch({
          city: name,
          map: new AMap.Map('container', {
            resizeEnable: true
          })
        })
        AMap.event.addListener(autocomplete, 'select', function(res) {
          const data = res.poi
          // 组合需要的数据
          that.dataRes = {
            location: data.location.lng + ',' + data.location.lat,
            address: data.name,
            addressDetail: data.district + '' + data.address
          }
          // 搜索
          placeSearch.search(data.name)
        })
      })
    }
  }
}
</script>

<style>
// 设置权重值,使搜索下拉选项不被隐藏
.amap-sug-result {
  z-index: 9999 !important;
}
</style>
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值