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: () => {
              // 请求完成后的操作
            },
          });
        },
      });

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

希望可以帮到你~~

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

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

加个关注不迷路~

相关推荐
zheshiyangyang3 小时前
uni-app学习【pages】
前端·学习·uni-app
小周同学:21 小时前
【UniApp打包鸿蒙APP全流程】如何配置并添加UniApp API所需的鸿蒙系统权限
华为·uni-app·harmonyos
初遇你时动了情2 天前
uniapp vue3 ts自定义底部 tabbar菜单
前端·javascript·uni-app
韩沛晓2 天前
uniapp跨域怎么解决
前端·javascript·uni-app
咸虾米2 天前
微信小程序服务端api签名,安全鉴权模式介绍,通过封装方法实现请求内容加密与签名
vue.js·微信小程序·uni-app
Ratten2 天前
使用 uniapp 实现的扫雷游戏
uni-app
YuShiYue2 天前
【uni-app】自定义导航栏以及状态栏,胶囊按钮位置信息的获取
uni-app·notepad++
2501_915921432 天前
iOS 应用上架多环境实战,Windows、Linux 与 Mac 的不同路径
android·ios·小程序·https·uni-app·iphone·webview
yede2 天前
uniapp - 自定义页面的tabBar
vue.js·uni-app
谢泽豪2 天前
解决 uniapp 修改index.html文件不生效的问题
前端·uni-app