首页 > 其他分享 >vue中实现高德 地图定位功能

vue中实现高德 地图定位功能

时间:2023-01-18 10:44:15浏览次数:43  
标签:result 定位 vue console log status location 高德 AMap

index.html

<template>
  <div id="app">
    <div id="container"></div>
    <div>
      <p>经度:{{ location.lng }}</p>
      <p>纬度:{{ location.lat }}</p>
      <p>地址:{{ location.address }}</p>
    </div>
  </div>
</template>

<script>
export default {
  name: 'App',

  data() {
    return {
      location: {
        lng: '', // 经度
        lat: '', // 纬度
        address: '',
      },
      // geoCoder: ''
    }
  },
  mounted() {
    this.getLocation()
    // this.getLngLatLocation()
  },
  methods: {
    getLocation() {
      const _this = this
      AMap.plugin(['AMap.Geolocation', 'AMap.Geocoder'], function () {
        var geolocation = new AMap.Geolocation({
          // 是否使用高精度定位,默认:true
          enableHighAccuracy: true,
          // 设置定位超时时间,默认:无穷大
          timeout: 10000,
          // 定位按钮的停靠位置的偏移量,默认:Pixel(10, 20)
          buttonOffset: new AMap.Pixel(10, 20),
          //  定位成功后调整地图视野范围使定位位置及精度范围视野内可见,默认:false
          zoomToAccuracy: true,
          //  定位按钮的排放位置,  RB表示右下
          buttonPosition: 'RB',
        })

        // var map = new AMap.Map("container", {
        //   resizeEnable: true
        // })
        // 地址逆解析
        var geocoder = new AMap.Geocoder({
          city: '010', //城市设为北京,默认:“全国”
          radius: 1000, //范围,默认:500
        })
        // var marker = new AMap.Marker();
        function regeoCode() {
          var lnglat = [_this.location.lng, _this.location.lat]
          // map.add(marker)
          // marker.setPosition(lnglat)
          geocoder.getAddress(lnglat, function (status, result) {
            if (status === 'complete' && result.regeocode) {
              console.log('查询地址成功', result)
              _this.location.address = result.regeocode.formattedAddress
            } else {
              console.log('根据经纬度查询地址失败', result)
            }
          })
        }

        geolocation.getCurrentPosition(function (status, result) {
          console.log('定位状态和结果', status, result)
          if (status == 'complete') {
            console.log('定位成功', result)
            // _this.signAddress = result.formattedAddress
            _this.location.lat = result.position.lat
            _this.location.lng = result.position.lng
            // _this.getAddress()
            regeoCode()
          } else {
            console.log('定位失败:' + result.message)
            _this.getLngLatLocation()
          }
        })
      })
    },
    // // IP定位获取当前城市信息
    // getLngLatLocation() {
    //   AMap.plugin("AMap.CitySearch", function () {
    //     console.log('进入获取城市信息')
    //     var citySearch = new AMap.CitySearch()
    //     citySearch.getLocalCity(function (status, result) {
    //       console.log('获取城市信息', status, result)
    //       if (status === "complete" && result.info === "OK") {
    //         // 查询成功,result即为当前所在城市信息
    //         console.log('当前城市信息', result)
    //       }
    //     })
    //   })
    // },
  },
}
</script>

<style>
#app {
  font-family: Avenir, Helvetica, Arial, sans-serif;
  -webkit-font-smoothing: antialiased;
  -moz-osx-font-smoothing: grayscale;
  text-align: center;
  color: #2c3e50;
  margin-top: 60px;
}
</style>


标签:result,定位,vue,console,log,status,location,高德,AMap
From: https://www.cnblogs.com/hxy--Tina/p/17059340.html

相关文章

  • 3、app自动化:使用appium定位元素的方式及元素的常用操作
    前提:没有的包,要先进行对应包的安装如:pipinstallAppium-Python-Client一、定位元素,包括属性定位和xpath定位方式a\属性定位属性定位方式   示例content......
  • vuex详解
    vuex的介绍vuex是vue.js应用程序中的状态管理模式,它是集中式存储管理所有组件的数据状态,vuex解决了多个视图之间的数据交互同步,不需要进行组件连接再传递数据。vuex的5大......
  • vue的基本使用
                                          ......
  • vue打包失败
    如题,vue项目打包输入npmrunbuild报错了  首先本地启动项目npmrundev是能正常跑起来的,看package.json  正确的命令应该是npmrunbuild:prod--report参......
  • vuejs从入门到精通——观察 Vue 实例从创建到销毁的完整生命周期
    观察Vue实例从创建到销毁的完整生命周期一、一个简单的Vue实例代码如下:<!DOCTYPEhtml><html><head><metacharset="utf-8"><title>Vue从入门到精通,https://w......
  • fixed 定位设置 scroll 不滚动
    我的问题是把容器的高度设置成了100vw,和视口保持同样的高度,所以设置scroll也无法滚动。尽管我试了很多其他方法都不能让其滚动。把高度设置成100%就可以了,结构如下:<......
  • Vue 项目报错:Syntax Error:Unexpected token
    报错ERRORin./node_modules/babel-loader/lib!./node_modules/vue-loader/lib/selector.js?type=script&index=0!./node_modules/element-ui/packages/image/src/image-......
  • vuejs从入门到精通——安装 vue 和 引入vue
    安装vue 和引入vue一、如何引入Vue.js1.1、安装cnpm:npminstall-gcnpm--registry=https://registry.npm.taobao.org1.2、使用cnpm安装vue.jsWindo......
  • vue的自定义过滤器 - Filter
    vue的自定义过滤器-Filter一、过滤器的介绍二、局部过滤器1.定义2.使用2.1基础用法2.2串联用法2.3接收参数三、全局过滤器1.定义2.使用四、总结一、过滤器的介绍V......
  • Vue+echart 展示后端获取的数据
    最近在合作做一个前后端分离项目时,为了测试我写的后端部分获取数据的效果,自己也学了一下vue的知识,在获取json信息这里也踩了很多坑。这里列举下我返回的json部分信......