uniapp使用谷歌地图获取位置

最近在使用uniapp开发的时候,由于客户是国外的,要求使用谷歌地图获取位置,所以找了一下实现的方法

首先在需要在需要使用地图的页面引入

bash 复制代码
let that = this
uni.getLocation({
        type: "wgs84",
        success: function (res) {
          console.log("当前位置的经度:" + res.longitude);
          console.log("当前位置的纬度:" + res.latitude);
          console.log(res.latitude, res.longitude, "res", res);
         
          const latlng = {
            lat: res.latitude,
            lng: res.longitude,
          };
          // 根据经纬度获取地址信息
          const apiKey = "api的key";
          const url = `https://maps.googleapis.com/maps/api/geocode/json?latlng=${res.latitude},${res.longitude}&key=api的key`;
          console.log(url);
          uni.request({
            url: 'https://maps.googleapis.com/maps/api/geocode/json?latlng=' + res.latitude + ',' + res.longitude + '&key=' + apiKey,
            method: "GET",
            success: (res2) => {
              console.log(res2)
            },
            fail: (err) => {
              // 处理网络错误
              this.errorMessage = `网络请求失败: ${err.errMsg}`;
            },
            complete: () => {
              // 请求完成后的操作
            },
          });
        },
      });

有相同问题的小伙伴可以试一下

希望可以帮到你~~

看到这如果对你有帮助的话

请用你发财的小手点赞加关注叭~

加个关注不迷路~

相关推荐
Rysxt_2 天前
UniApp五大UI框架与uni-ui核心区别对比
uni-app·uni-ui
2501_915918412 天前
HTTPS 代理失效,启用双向认证(mTLS)的 iOS 应用网络怎么抓包调试
android·网络·ios·小程序·https·uni-app·iphone
2501_915106322 天前
混合应用(Hybrid)安全加固,不依赖源码对成品 IPA 混淆
android·安全·小程序·https·uni-app·iphone·webview
00后程序员张2 天前
无需越狱,来对 iOS 设备进行调试、管理与分析
android·ios·小程序·https·uni-app·iphone·webview
芒果大胖砸2 天前
uniapp当中如何实现长按复制功能并且能够自由选择内容
开发语言·javascript·uni-app
00后程序员张2 天前
在 iOS 上架中如何批量方便快捷管理 Bundle ID
android·ios·小程序·https·uni-app·iphone·webview
换日线°2 天前
uni-app对接腾讯即时通讯 IM
前端·uni-app
咚咚?2 天前
uniapp [JS Framework] 当前运行的基座不包含原生插件[xxxx],请在manifest中配置该插件,重新制作包括该原生插件的自定义运行基座
开发语言·javascript·uni-app
木子啊2 天前
Uni-app生命周期:执行顺序与避坑指南
前端·javascript·uni-app
快起来搬砖了2 天前
UniApp 全端兼容 OSS 视频上传实现方案
小程序·uni-app·app·h5·文件上传·oss文件上传