高德地图定位

//在index.html页面引入

<script src="http://webapi.amap.com/maps?v=1.3&key=高德地图的key"></script>

配置外置引入,不会被打包进项目包
externals: {

    'AMap': 'AMap'

 },
//在组件页面引入
import AMap from 'AMap'

// 开始定位方法
    readyPosition(){
      let _this = this
      let map,geolocation
      map = new AMap.Map('iCenter')
      map.plugin('AMap.Geolocation', function() {
        
        geolocation = new AMap.Geolocation({
          timeout: 1000, //若在指定时间内未定位成功,返回超时错误信息“TIMEOUT”
          GeoLocationFirst: true, //默认为false,设置为true的时候可以调整PC端为优先使用浏览器定位,失败后使用IP定位
          maximumAge: 0, //定位结果缓存0毫秒,默认:0
          extensions: 'base' //将得到的经纬度进行逆地理编码后获取地址信息
        });
        map.addControl(geolocation)
        geolocation.getCurrentPosition()
        AMap.event.addListener(geolocation, 'complete', function(data) {
          let addComp = data.addressComponent
          _this.posProvice = addComp.province
          _this.posCity = addComp.city
          _this.district = addComp.district
          _this.street = addComp.township
          //利用vuex设置公共值,然后用localStorage存储省市区街道到本地
          _this.setUserInfo({ positionProvice: _this.posProvice })
          _this.setUserInfo({ positionCity: _this.posCity })
          _this.setUserInfo({ positionDistrct: _this.district })
          _this.setUserInfo({ positionStreet: _this.street })
          //判断当前是否选择了区、街道
          if(_this.userInfo.selectDistrct=='' || _this.userInfo.selectDistrct==undefined){
            _this.areaStreet = _this.district+""+_this.street
          }else{
            _this.areaStreet =    _this.userInfo.selectDistrct+""+_this.userInfo.selectStreet
          }
          //定位城市
          if (_this.posCity != _this.userInfo.selectCity) {
            _this.$refs.positionCity.show()
          }
        }); //返回定位信息
        AMap.event.addListener(geolocation, 'error', function(data) {
          if (data.info == 'FAILED') {
            // _this.prompt('获取您当前位置失败')
          }
        });
      });
    },

评论 2
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

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

抵扣说明:

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

余额充值