vue+antdesign 高德地图根据名称选点功能,并去除logo 封装组件

高德地图申请key及密钥

注册账号并申请key官方地址
  1. 在vue项目index.html文件中引入
<script type="text/javascript">
        window._AMapSecurityConfig = {
            securityJsCode:'xxxxx',  // 自己申请的密钥
        }
</script>
  <script type="text/javascript" src="https://webapi.amap.com/maps?v=1.4.15&key=自己申请的key&plugin=AMap.Geolocation,AMap.Weather,AMap.MarkerClusterer,AMap.DistrictSearch,AMap.DistrictLayer,AMap.Autocomplete,AMap.PlaceSearch"></script>

2.在components文件夹下新建coordinatePick/index.vue,文件内容如下(文件中crec是基于antdesign封装的组件用法和antdesign一样)

<template>
  <div>
    <crec-modal v-drag title="选择坐标" v-model="open" width="50%">
      <crec-form-model
        ref="ruleForm"
        :model="queryParams"
        :label-col="labelCol"
        :wrapper-col="wrapperCol"
      >
        <crec-form-model-item label="坐标获取结果">
          <crec-input
            v-model="queryParams.longitudeAndLatitude"
            placeholder="坐标获取结果"
            disabled
            ref="inputRef"
            allowClear
          />
        </crec-form-model-item>
        <crec-form-model-item label="按关键字搜索">
          <crec-select
            v-model="queryParams.keyword"
            showSearch
            placeholder="按关键字搜索"
            style="width: 100%"
            :defaultActiveFirstOption="false"
            :showArrow="false"
            :filterOption="false"
            @search="remoteMethod"
            @change="selectChang"
            :notFoundContent="null"
          >
            <crec-select-option
              v-for="item in searchArr"
              :key="item.value"
              :label="item.label"
              :value="item.value"
            >{{ item.label }}</crec-select-option>
          </crec-select>
        </crec-form-model-item>
      </crec-form-model>
      <div class="mapContent" id="pointMap"></div>
      <template slot="footer">
        <crec-button @click="open = false">关闭</crec-button>
        <crec-button type="primary" @click="handleSubmit">保存</crec-button>
      </template>
    </crec-modal>
  </div>
</template>
<script>
import AMap from 'AMap'
export default {
  name: 'CoordinatePick',
  data () {
    return {
      queryParams: {
        keyword: '',
        longitudeAndLatitude: ''
      },
      fetching: false,
      searchArr: [],
      Map: null,
      open: false,
      labelCol: { span: 4 },
      wrapperCol: { span: 20 }
    }
  },
  methods: {
    initMap (longitudeAndLatitude) {
      this.queryParams.keyword = ''
      this.open = true
      this.$nextTick(() => {
        if (this.Map) {
          this.Map.clearMap()
        }
        this.Map = new AMap.Map('pointMap', {
          // 设置地图容器id
          // viewMode: "3D",    //是否为3D地图模式
          draggable: false,
          zoom: 11 // 初始化地图级别
          // center: [105.602725, 37.076636], //初始化地图中心点位置
        })
        if (longitudeAndLatitude) {
          // 之前有坐标,需要处理回显
          this.queryParams.longitudeAndLatitude = longitudeAndLatitude
          // 创建一个 Marker 实例:
          const marker = new AMap.Marker({
            draggable: false,
            position: new AMap.LngLat(
              Number(longitudeAndLatitude.split(',')[0]),
              Number(longitudeAndLatitude.split(',')[1])
            ) // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
          })
          this.Map.add(marker)
        }
        this.Map.on('click', (e) => {
          console.log(e, e.lnglat.lng, e.lnglat.lat, 'eeeeeeeeeeeee')
          this.Map.clearMap()
          this.queryParams.longitudeAndLatitude = e.lnglat.lng + ',' + e.lnglat.lat
          const marker = new AMap.Marker({
            draggable: false,
            position: new AMap.LngLat(e.lnglat.lng, e.lnglat.lat) // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
          })
          this.Map.add(marker)
        })
        this.Map.setFitView()
      })
    },
    remoteMethod (query) {
      console.log(query, 'query')
      if (query) {
        AMap.plugin('AMap.Autocomplete', () => {
          // 实例化Autocomplete
          var autoOptions = {
            city: '全国'
          }
          var autoComplete = new AMap.Autocomplete(autoOptions)
          autoComplete.search(query, (status, result) => {
            if (result && result.info === 'OK') {
              this.searchArr = result.tips
                .map((v) => {
                  if (v.location) {
                    return {
                      label: v.name,
                      value: v.location.lng + ',' + v.location.lat
                    }
                  }
                })
                .filter((ele) => ele)
            }
          })
        })
      } else {
        this.searchArr = []
      }
    },
    handleSubmit () {
      this.$emit('lngLatChange', this.queryParams.longitudeAndLatitude)
      this.open = false
    },
    selectChang () {
      if (this.Map) {
        this.Map.clearMap()
      }
      this.queryParams.longitudeAndLatitude = this.queryParams.keyword
      // 创建一个 Marker 实例:
      const marker = new AMap.Marker({
        draggable: false,
        position: new AMap.LngLat(this.queryParams.keyword.split(',')[0], this.queryParams.keyword.split(',')[1]) // 经纬度对象,也可以是经纬度构成的一维数组[116.39, 39.9]
      })
      this.Map.add(marker)
      this.Map.setFitView()
    },
    handleCancel (e) {
      console.log('Clicked cancel button')
      this.visible = false
    }
  }
}
</script>
<style lang="less" scoped>
.mapContent {
  width: 100%;
  height: 500px;
}
/deep/ .amap-logo {
  display: none !important;
  visibility: hidden !important;
}

/deep/ .amap-copyright {
  display: none !important;
  visibility: hidden !important;
}
</style>

3.在需要的地方引用,注册

<coordinatePick ref="coordinatePickRef" @lngLatChange="lngLatChange"></coordinatePick> // 模板中
import coordinatePick from '@/components/coordinatePick'
components: {
    coordinatePick
  }
  // 方法里面
lngLatChange (lnglat) {
      console.log(lnglat) // 这里就是返回的用逗号拼接好的经纬度坐标
    }

效果图

Ant Design VueVue.js 的一套UI组件库。其表单组件提供了resetFields()方法,实现表单重置的功能。如果使用resetFields()方法后,表单并没有重置,一般有以下几个可能的原因: 1. resetFields()方法没有被正确调用。 首先,要注意确认resetFields()方法的调用时机是否正确。例如,表单提交后需要调用resetFields()方法进行重置,如果在其他时刻调用,会导致表单没有重置。 在调用resetFields()方法时,也要注意方法的参数。如果传入了表单项的name属性,只会重置指定的表单项。如果不传入参数,则会重置整个表单。 2. 表单项属性被错误配置。 另外,表单项的属性也可能会影响重置功能。例如,如果表单项本身设置了默认值并且没有设置表单项属性,那么resetFields()方法将不会清除表单项。需要将该表单项的属性设置为值为空字符串或null。 3. 组件版本过低。 如果以上两种情况都不是原因,还要考虑Ant Design Vue组件库本身的问题。可能是组件库的版本太低,需要更新到最新版本才能解决问题。 总的来说,解决Ant Design Vue Form表单resetFields()方法没有生效的问题,要根据具体情况分析,确认方法调用时机正确、表单项的属性设置正确以及组件版本是否过低等因素。这样,在正确配置和使用resetFields()方法的情况下,重新渲染表单项内容才能生效。
评论
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值