uni-app微信小程序如何使用高德地图。通过经纬度获取所在城市

高德地图官方是这样介绍的使用方法可以参考:入门指南-微信小程序插件 | 高德地图API

我再介绍一下我得具体应用。

1,首先要在申请高德地图开放平台得账号。然后在这个账号中申请一个应用。类型选择微信小程序。

我的应用 | 高德控制台

获取Key-创建工程-开发指南-微信小程序插件 | 高德地图API

点击提交我们就得到一个可以看的key得应用啦。

3,然后去相关下载-微信小程序插件 | 高德地图API

下载之后你会得到一个压缩文件解压缩后得到一个amap-wx.130.js。把他放到自己的项目里面。

4,

因为他最后是通过

复制代码
module.exports.AMapWX=AMapWX;得方式导出的。如果在vue3项目种我们通过var amapFile = require('@/js_sdk/amap/amap-wx.130.js');得方式引入他会报错

vendor.js? [sm]:2643 Error: module 'components/get-location/@/js_sdk/amap/amap-wx.130.js' is not defined, require args is '@/js_sdk/amap/amap-wx.130.js'

复制代码
于是我修改了amap-wx.130.js得导出方式。把module.exports.AMapWX=AMapWX;改成export default{ AMapWX };

然后引入方式改成import就可以了。

复制代码
import {onLoad, onInit, onShow} from "@dcloudio/uni-app";
import {ref} from "vue";
复制代码
 import amapFile  from "@/js_sdk/amap/amap-wx.130.js"
复制代码
const nowCity = ref('');
复制代码
onLoad((options: any) => {
  checkauthorize();
})
复制代码
const checkauthorize = () => {
  uni.getSetting({
    success(res) {
      if (!res.authSetting['scope.userLocation']) {
        uni.authorize({
          scope: 'scope.userLocation',
          success: () => { //这里是用户同意授权后的回调
            getLocation();
          },
          fail: () => { //这里是用户拒绝授权后的回调
            rejectSetting()
          }
        })
      } else { //用户已经授权过了
        getLocation();
      }
    }
  })
}

const getLocation = () => {
  uni.getLocation({
    type: 'wgs84',
    success: function (res) {
      console.log(res)
      console.log('经度:' + res.longitude);
      console.log('纬度:' + res.latitude);
      getAddress(res.latitude,res.longitude);
    }
  });
}

//经纬度转换成省市区 latitude纬度,long经度
const getAddress=(latitude, longitude)=>{
  let myAmapFun = new amapFile.AMapWX({ key: "高德地图的key值" });
  myAmapFun.getRegeo({
    location: '' + longitude + ',' + latitude + '',//location的格式为'经度,纬度'
    success: function (data) {
      console.log("转换成省市",data);
      let {province,city,district} = data[0].regeocodeData.addressComponent;
      city = (city || city?.length>0) ? city:"";
      console.log(city)
      nowCity.value=city;
      uni.setStorageSync('city',nowCity.value)
      console.log("省市区:",province+city+district)
    },
    fail: function (info) { }
  })
}


//用户拒绝授权后的回调
const rejectSetting = () => {
  var that = this;
  uni.showModal({
    title: '警告',
    content: '授权失败,请打开位置消息授权',
    success: (res) => {
      if (res.confirm) { //去授权
        toOpenSetting();
      } else if (res.cancel) {//用户点击取消
        console.log(res);
        uni.showModal({
          title: '提示',
          content: '获取权限失败,将无法获取定位哦~',
          showCancel: false,
          success: (res) => {
            toOpenSetting();
          }
        })
      }
    }
  })
}
//打开微信设置页面
const toOpenSetting = () => {
  uni.openSetting({
    success: (e) => {
      console.log(e);
    }
  })
}

同时还有一些地方要改。

1,首先微信开放平台得小程序后台设置种。开发管理-接口权限。开通获取当前的地理位置。

因为这个小程序的类目不在申请的列表里。所以下图显示的是没有权限。如果你也是这样的情况可以先去添加小程序的类目(首页-小程序类目-查看详情)

2,开发管理-开发设置里面添加request合法域名: https://restapi.amap.com

3.代码里面要添加manifest.json里面加

复制代码
"permission" : {
    "scope.userLocation" : {
        "desc" : "展示位置"
    }
}

4.代码里面。page.json在使用地图的页面加上

复制代码
"permission": {
  "scope.userLocation": {
    "desc": "你的位置信息将用于小程序位置接口的效果展示"
  }
}

小程序中回调起询问会话框。点击确定就能获取地理位置了

相关推荐
weixin_lynhgworld4 小时前
从闲置到珍宝:旧物回收小程序系统重塑物品价值
小程序·旧物回收
2501_916007479 小时前
iOS App 上架实战 从内测到应用商店发布的全周期流程解析
android·ios·小程序·https·uni-app·iphone·webview
小小怪下士_---_14 小时前
uniapp开发微信小程序自定义导航栏
前端·vue.js·微信小程序·小程序·uni-app
摸着石头过河的石头16 小时前
小程序调试全攻略:微信/支付宝避坑指南,小白也能一次通关
前端·微信小程序
fakaifa1 天前
点大餐饮独立版系统源码v1.0.3+uniapp前端+搭建教程
小程序·uni-app·php·源码下载·点大餐饮·扫码点单
Dignity_呱1 天前
如何在不发版时,实现小程序的 AB 测试?
前端·面试·微信小程序
说私域2 天前
基于开源 AI 大模型 AI 智能名片 S2B2C 商城小程序视角下的企业组织能力建设与破圈升级
人工智能·小程序
fakaifa2 天前
【最新版】CRMEB Pro版v3.4系统源码全开源+PC端+uniapp前端+搭建教程
人工智能·小程序·uni-app·php·crmeb·源码下载·crmebpro
2501_915918412 天前
iOS 应用上架全流程实践,从开发内测到正式发布的多工具组合方案
android·ios·小程序·https·uni-app·iphone·webview
上海云盾第一敬业销售2 天前
小程序被爬虫攻击,使用waf能防护吗?
爬虫·小程序