vue实现高德地图模糊搜索+筛选条件+定位+信息窗(完整案例)

vue实现高德地图模糊搜索+筛选条件+定位+信息窗

vue实现高德地图模糊搜索+筛选条件+定位+信息窗(完整案例)

先npm安装高德地图
npm i @amap/amap-jsapi-loader --save

<template>
  <div class="StationMap">
    <van-nav-bar title="场站地图" fixed placeholder safe-area-inset-top left-text="返回" left-arrow @click-left="$router.goBack()"/>
    <div class="content-header">
      <div class="van-search-box">
        <van-search v-model="searchValue" placeholder="请输入场站名称" @search="onSearch"></van-search>
      </div>
      <van-dropdown-menu>
        <van-dropdown-item v-model="dropdownValue" :options="dropdownOption"  @change="dropdownChange"/>
      </van-dropdown-menu>
    </div>
    <ul class="searchData" v-show="searchDataShow">
      <li class="van-cell" v-for="(item, i) in searchData" :key="i" @click="searchDataClick(item)">{{item.content}}</li>
    </ul>

    <div id="container"></div>
  </div>
</template>

<script>
import AMapLoader from '@amap/amap-jsapi-loader';

export default {
  name: "StationMap",
  computed: {
    searchData () {
      let searchVal = '';//搜索后的数据
      if (this.searchValue) {
        searchVal = this.mapData.filter(item => {
          return [Object.keys(item)[2]].some(key => {
            // 搜索所有的内容
            return String(item[key]).toLowerCase().indexOf(this.searchValue.toLowerCase()) > -1;
          })
        })
        this.searchDataShow = true;
        return searchVal;
      }
    }
  },
  watch: {
    searchDataShow () {
      if ((this.searchDataShow) && (this.searchValue === this.searchDataClickItem)) this.searchDataShow = false
    }
  },
  data() {
    return {
      map: null,
      searchDataShow: false,
      searchDataClickItem: "", // 点击之后的值
      mapData: [
        {
          lng: "106.15",
          lat: "30.02",
          content: "a合川"
        },
        {
          lng: "106.16",
          lat: "29.18",
          content: "b江津"
        },
        {
          lng: "107.05",
          lat: "29.10",
          content: "c南川"
        },
      ],
      searchValue: "", // 搜索的值

      dropdownValue: 1,
      dropdownOption: [
        {text: '项目1', value: 1},
        {text: '项目2', value: 2},
      ],
    }
  },
  methods: {
    dropdownChange (val) {
      this.searchValue = "";
    },
    // 搜索按钮, 键盘Enter触发
    onSearch(val) {
      this.mapData.forEach(item => {
        if (item.content === val) {
          this.searchDataClick(item)
        }
      })
    },
    // 点击模糊搜索列表 或 弹出信息窗并设置中心位置
    searchDataClick (item) {
      this.searchValue = item.content;
      this.searchDataShow = false;
      this.searchDataClickItem = item.content;

      // 1.信息窗的偏移位
      let infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
      // 2.获取搜索结果的位置
      let position = [item.lng, item.lat]
      // 3.设置信息窗的内容
      infoWindow.setContent(item.content);
      // 4.将 搜索结果的位置 设置为 中心点
      this.map.setCenter(position)
      // 5.打开信息窗
      infoWindow.open(this.map, position);
    },
    // 高德地图
    initMap() {
      AMapLoader.load({
        key: "4a9b8d4819c66a63a4b5544397510c59", // 申请好的开发者Key
      }).then(AMap => {
        this.map = new AMap.Map("container", {
          resizeEnable: true,
        });
        let lnglats = [];
        for (const i in this.mapData) {
          lnglats.push([this.mapData[i].lng, this.mapData[i].lat])
        }
        for (let i = 0; i < this.mapData.length; i++) {
          let marker = new AMap.Marker({
            position: lnglats[i],
            map: this.map
          });
          marker.content = this.mapData[i].content;
          marker.on('click', this.markerClick);
          marker.emit('click', {target: marker});
        }
        this.map.setFitView();
      }).catch(err => {
        console.log(err);
      })
    },
    markerClick (e) {
      let infoWindow = new AMap.InfoWindow({offset: new AMap.Pixel(0, -30)});
      infoWindow.setContent(e.target.content);
      infoWindow.open(this.map, e.target.getPosition());
    }
  },
  mounted() {
    //DOM初始化完成进行地图初始化
    this.initMap();
  }
}
</script>

<style lang="scss">
.StationMap {
  .content-header {
    width: 100%;
    position: fixed;
    z-index: 999;
    display: flex;
    .van-search-box{
      flex: 1;
      .van-search {
        padding: 0;
        .van-cell {
          line-height: 40px;
          padding: 0;
        }
        .van-icon-clear{
          padding-right: 20px;
        }
      }
    }
    .van-dropdown-menu {
      flex: 1;
      //margin-left: 10px;
      border-left: 1px solid #e8e8ef;
      .van-dropdown-menu__bar {
        height: 40px;
        background-color: #f7f8fa;
        box-shadow: none;
      }
    }
  }
  .searchData {
    position: fixed;
    top: 86px;
    right: 0;
    left: 0;
    z-index: 998;
    overflow: hidden;
    background-color: #fff;
  }
  #container {
    width: 100%;
    height: 100vh;
  }
}
</style>

结果
在这里插入图片描述

  • 1
    点赞
  • 20
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 5
    评论
要使用高德地图实现模糊搜索定位,你需要先在项目中引入高德地图的 JavaScript API,并且需要获取高德地图的 API key。接下来,你可以使用 Autocomplete 插件实现模糊搜索,具体实现步骤如下: 1. 在 Vue 3 中安装高德地图的 JavaScript API。 ```bash npm install @amap/amap-jsapi-loader --save ``` 2. 在 main.ts 文件中引入高德地图的 JavaScript API,并在创建 Vue 实例之前加载 JavaScript API。 ```typescript import { createApp } from 'vue' import App from './App.vue' import AMapLoader from '@amap/amap-jsapi-loader' AMapLoader.load({ key: 'your_amap_api_key', version: '2.0', plugins: ['AMap.Autocomplete'] }).then(() => { createApp(App).mount('#app') }) ``` 3. 在组件中使用 Autocomplete 插件实现模糊搜索。 ```vue <template> <div> <input v-model="address" placeholder="请输入地址" /> <ul> <li v-for="suggestion in suggestions" :key="suggestion.id">{{ suggestion.name }}</li> </ul> </div> </template> <script lang="ts"> import { defineComponent, ref } from 'vue' import AMapLoader from '@amap/amap-jsapi-loader' export default defineComponent({ name: 'AutoCompleteDemo', setup() { const address = ref('') const suggestions = ref([]) const search = async () => { const AMap = await AMapLoader.load({ key: 'your_amap_api_key', version: '2.0', plugins: ['AMap.Autocomplete'] }) const autoComplete = new AMap.Autocomplete({ city: '全国' }) autoComplete.search(address.value, (status, result) => { if (status === 'complete' && result.tips) { suggestions.value = result.tips } }) } return { address, suggestions, search } } }) </script> ``` 在这个例子中,我们使用了 ref 函数来创建了两个响应式变量 address 和 suggestions,分别用于存储输入的地址和搜索结果。我们还定义了一个 search 函数,在用户输入时触发,它会通过 AMapLoader.load() 方法加载 Autocomplete 插件,并使用 Autocomplete.search() 方法进行模糊搜索。当搜索完成时,Autocomplete.search() 的回调函数会返回搜索结果,我们将结果存储在 suggestions 变量中,并在页面中渲染出来。 注意,这里我们设置了 city 属性为全国,表示搜索范围为全国。你也可以根据需要修改为特定的城市或区域。
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包

打赏作者

码上暴富

你的鼓励将是我创作的最大动力

¥1 ¥2 ¥4 ¥6 ¥10 ¥20
扫码支付:¥1
获取中
扫码支付

您的余额不足,请更换扫码支付或充值

打赏作者

实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

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

余额充值