【vue2高德地图api】04-poi搜索

系列文章目录



前言

提示:这里可以添加本文要记录的大概内容:

本篇要实现的功能,看下图
在这里插入图片描述


提示:以下是本篇文章正文内容,下面案例可供参考

一、高德地图文档入口

配合文档一起食用更好
Web服务概述

二、使用步骤

1.创建文件以及路由

在这里插入图片描述

路由如下:

{
  path: 'search',
  name: 'parkSearch',
  component: () => import('../views/park/search.vue'),
  meta: {
    title: '搜索周边公园',
    keepAlive: false,
  },
},

在这里插入图片描述

2.编写页面代码

代码如下(示例):

<div class="container">
    <van-search v-model="searchValue" show-action placeholder="请输入搜索关键词" @input="onSearch" @cancel="onCancel" />
    <main class="main">
      <div class="title">热门搜索</div>
      <div class="park-list">
        <map-item v-for="item in parkList" :key="item.id" :item="item">
          <p>
            距离
            <span style="color: #3399cc">
              {{
                getDistances(position[1], position[0], item.location.split(',')[1], item.location.split(',')[0]).km
              }}千米
            </span>
            &nbsp;
            {{ item.address }}
          </p>
        </map-item>
      </div>
    </main>
  </div>

这是已经写好的变量,后面我会补充,并且按照步骤来

3.样式

<style scoped lang="scss">
.container {
  .main {
    height: calc(100vh - 108px);
    overflow-y: auto;
    .title {
      background-color: #eee;
      color: #333;
      font-size: 28px;
      padding: 10px 30px;
    }
    .park-list {
      padding: 20px;
    }
  }
}
</style>

4变量以及方法

data变量

      searchValue: '', // 无作用
      queryParams: {
        keywords: '公园',
        types: '110000',
        children: 1,
        city: '长沙',
        page_num: 1,
        page_size: 20,
      },
      parkList: [], // 查询列表
      position: [], // 当前定位

import 引入方法

import AMapLoader from '@amap/amap-jsapi-loader';

// 首先,导入lodash库的debounce函数
import { debounce } from 'lodash';

import { mapPlaceText } from '@/api/map.js';

因为,我做的搜索栏是实时的,但是要加一个过滤时间,就是搜索输入框2秒钟后再执行查询方法。

methods方法

init() {
      this.initMap();
      this.getList();
    },
    initMap() {
      AMapLoader.load({
        key: this.mapJsKey, // 申请好的Web端开发者Key,首次调用 load 时必填
        //2.0版本太卡了 ,所以使用的1.4.0版本  其插件也有不同  如:ToolBar
        version: '1.4.0', // 指定要加载的 JSAPI 的版本,缺省时默认为 1.4.15
        resizeEnable: true, // 定位到当前位置
        plugins: [
          'AMap.Geolocation', //定位
        ], // 需要使用的的插件列表,如比例尺'AMap.Scale'等
      })
        .then((AMap) => {
          this.geolocation = new AMap.Geolocation({
            //定位
            enableHighAccuracy: true, //是否使用高精度定位,默认:true
            timeout: 5000, //超过10秒后停止定位,默认:无穷大
            maximumAge: 0, //定位结果缓存0毫秒,默认:0
            convert: true, //自动偏移坐标,偏移后的坐标为高德坐标,默认:true
            showButton: true, //显示定位按钮,默认:true
            buttonPosition: 'RB', //定位按钮停靠位置,默认:'LB',左下角
            buttonOffset: new AMap.Pixel(60, 20), //定位按钮与设置的停靠位置的偏移量,默认:Pixel(10, 20)
            showMarker: true, //定位成功后在定位到的位置显示点标记,默认:true
            showCircle: true, //定位成功后用圆圈表示定位精度范围,默认:true
            panToLocation: true, //定位成功后将定位到的位置作为地图中心点,默认:true
            zoomToAccuracy: true, //定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
          });

          this.geolocation.getCurrentPosition((status, result) => {
            if (status == 'complete') {
              this.locationInfo = result;
              this.position = [result.position.lng, result.position.lat];
            } else {
              console.log('error: =>', result);
            }
          });
        })
        .catch((e) => {
          console.log(e);
        });
    },
    debouncedSearch: debounce(function () {
      //   console.log(this.queryParams.keywords, '防抖');
      this.queryParams.keywords = this.searchValue;
      this.getList();
    }, 1000),
    onSearch() {
      this.debouncedSearch();
    },
    onCancel() {
      this.$router.go(-1);
    },

    getList() {
      mapPlaceText(this.queryParams).then((res) => {
        this.parkList = res.pois;
      });
    },

5.编写查询方法

这个在前面的教程里有
在这里插入图片描述

// 关键字搜索
export function mapPlaceText(params) {
  return new Promise((resolve, reject) => {
    axios({
      method: 'get',
      url: baseUrl + '/place/text',
      params,
    })
      .then((res) => {
        resolve(res.data);
      })
      .catch((err) => {
        reject(err);
      });
  });
}

可能要下载lodash这个,因为我做了一个延迟
在这里插入图片描述


总结

提示:这里对文章进行总结:

看了我之前的教程,肯定不会出错。因为我也是建了个新项目一步步来的。

  • 0
    点赞
  • 0
    收藏
    觉得还不错? 一键收藏
  • 打赏
    打赏
  • 1
    评论
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

打赏作者

阿民不加班

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

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

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

打赏作者

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

抵扣说明:

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

余额充值