H5移动端 引入高德地图(获取经纬度与地址带搜索反选

index.html引入JS 自己申请KEY<script type="text/javascript" src="http://webapi.amap.com/maps?v=1.3&key=0250860ccb5953fa5d655e8acf40ebb7&plugin=AMap.Geocoder"></script>

    <!-- <script type="text/javascript"
        src="https://webapi.amap.com/maps?v=1.4.5&key=e85674d8a91481719a16c2ccda50b51b&plugin=AMap.Geocoder"></script> -->



<template>
  <div id="rootMap">
    <div id="map"
         ref="map"></div>
    <input class="inputclass"
           v-model="address"
           placeholder="请输入地址" />
    <div class="box">
      <van-button type="primary"
                  native-type="button"
                  @click="geocoder()">搜索</van-button>
      <van-button type="primary"
                  native-type="button"
                  @click="SendLngLat()">确定</van-button>
    </div>
  </div>
</template>
<script>
import { Toast } from "vant";
// import BMap from "BMap";

export default {
  data () {
    return {
      timer: null,
      map: "",
      searchKey: "河南省信阳市",
      currentLonLat: [],
      address: "",
      lnglat: '',
    };
  },
  watch: {

  },
  mounted () {
    this.getMap();
  },

  methods: {

    getMap () {
      //创建地图
      this.map = new AMap.Map("map", {
        //mapStyle: 'amap://styles/grey',
        zoom: 13,
        center: [114.119, 32.116]
      });
      //地图点击事件
      this.map.on("click", this.showInfoClick);

    },
    showInfoClick (e) {
      this.map.clearMap();//清理地图
      let lng = e.lnglat.getLng(); //获取经度
      let lat = e.lnglat.getLat(); //获取纬度

      console.log(e, lng, lat);

      this.regeocoder([lng, lat])
      //marker点
      let marker = new AMap.Marker({
        position: [lng, lat],
        offset: new AMap.Pixel(-13, -30)
      });
      this.map.add(marker); //加载点
      this.map.setFitView(); //缩放至地图
      this.lnglat = lng + "," + lat;//将经纬度绑定至input输入框

    },

    // 通过经纬度获取地址
    regeocoder (loc) { //逆地理编码
      console.log(loc);
      var that = this
      var geocoder = new AMap.Geocoder({
        radius: 1000,
        extensions: "all"
      });
      geocoder.getAddress(loc, function (status, result) {
        if (status === 'complete' && result.info === 'OK') {
          console.log(result);

          var address = result.regeocode.formattedAddress; //返回地址描述
          that.address = address
          that.currentLonLat = [...loc, address];
          console.log(that.currentLonLat);

        }
      });
    },

    //搜索按钮事件 通过地址获取经纬度
    geocoder () {
      console.log(this.address);
      this.map.clearMap();
      var that = this
      var myGeo = new AMap.Geocoder();
      //地理编码,返回地理编码结果
      myGeo.getLocation(this.address, function (status, result) {
        if (status === 'complete' && result.info === 'OK') {
          //地址解析成功
          // var address = data.regeocode.formattedAddress;
          var x = result.geocodes[0].location.lng;
          var y = result.geocodes[0].location.lat;
          that.currentLonLat = [x, y, that.address];

          console.log(that.currentLonLat);

          that.getMap(x, y);
          // that.regeocoder(XY);
          that.punctuation(x, y); //更新标记
        } else {
          //地址解析失败
          alert("地址不存在")
        }
      });
    },

    // 标记点
    punctuation (x, y) {
      let marker = new AMap.Marker({
        position: [x, y],
        offset: new AMap.Pixel(-13, -30)
      });
      this.map.add(marker); //加载点
      this.map.setFitView(); //缩放至地图
    },

    //点击确定后把经纬度传给父vue
    SendLngLat () {
      console.log(this.currentLonLat.length);
      if (this.currentLonLat.length > 0) {
        this.$emit("SendLngLat", this.currentLonLat);
      } else {
        Toast("请点击地图上的点获取坐标!");
      }
    }
  }
};
</script>
<style lang="scss" scoped>
#rootMap {
  height: 100%;
  width: 100%;
  position: relative;
}
#map {
  height: 100%;
  width: 100%;
  position: relative;
}
.inputclass {
  position: absolute;
  width: 180px;
  height: 30px;
  top: 0;
  left: 0;
  font-size: 14px;
}

.box {
  position: absolute;
  left: 0;
  top: 40px;
}
</style>



	以下为父组件的组件引入

    <GaodeMap @SendLngLat="SendLngLat"></GaodeMap>

    // 接受从子组件传过来的值
    SendLngLat (data) {
      this.currentLonLatList = data;
      // console.log(this.currentLonLatList);

      if (this.currentLonLatList.length) {
        this.formInfo.data.sewerageUserAddress = this.currentLonLatList[2];
        this.formInfo.data.lon = this.currentLonLatList[0];
        this.formInfo.data.lat = this.currentLonLatList[1];
        this.showType6 = false;
      }
      // console.log(this.ruleform);
    },

在这里插入图片描述

  • 4
    点赞
  • 10
    收藏
    觉得还不错? 一键收藏
  • 5
    评论
在uni-app的H5项目中,可以使用高德地图API来获取当前定位。下面是一种实现方式: 1. 首先,在uni-app项目中安装高德地图插件。可以通过以下命令进行安装: ``` npm install @types/amap-js-api --save ``` 2. 在需要获取定位的页面中,引入高德地图的JS API。可以在`index.html`文件中添加以下代码: ```html <script src="https://webapi.amap.com/maps?v=1.4.15&key=your_amap_key"></script> ``` 其中,`your_amap_key`需要替换为你自己的高德地图开发者密钥。 3. 在页面的`methods`中,编写获取定位的方法。可以使用高德地图提供的`AMap.Geolocation`类来实现。以下是一个示例代码: ```javascript methods: { getLocation() { AMap.plugin('AMap.Geolocation', () => { const geolocation = new AMap.Geolocation({ enableHighAccuracy: true, // 是否使用高精度定位,默认为true timeout: 10000, // 超过10秒后停止定位,默认:无穷大 }); geolocation.getCurrentPosition((status, result) => { if (status === 'complete') { // 定位成功,result中包含经纬度等信息 const { lng, lat } = result.position; console.log('当前位置经纬度:', lng, lat); } else { // 定位失败 console.log('定位失败'); } }); }); } } ``` 4. 在页面中调用`getLocation`方法即可获取当前定位信息。可以在按钮的点击事件中调用该方法: ```html <button @click="getLocation">获取当前定位</button> ``` 这样,当用户点击按钮时,就会触发获取当前定位的操作,并将结果输出到控制台中。

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 5
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值