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

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

希望可以帮到你~~

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

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

加个关注不迷路~

相关推荐
影子打怪2 小时前
uniapp项目中,通过renderjs的方式展示地图,及其标点、轨迹展示、轨迹回放
uni-app
iOS阿玮5 小时前
想偷懒购买现成的应用,结果一更新就遇到了4.3a!
uni-app·app·apple
HashTang5 小时前
【AI 编程实战】第 4 篇:一次完美 vs 五轮对话 - UnoCSS 配置的正确姿势
前端·uni-app·ai编程
雯0609~15 小时前
uni-app:防止重复提交
前端·javascript·uni-app
2501_9159090615 小时前
苹果应用加密方案的一种方法,在没有源码的前提下,如何处理 IPA 的安全问题
android·安全·ios·小程序·uni-app·iphone·webview
百锦再15 小时前
与AI沟通的正确方式——AI提示词:原理、策略与精通之道
android·java·开发语言·人工智能·python·ui·uni-app
2501_9159090615 小时前
iOS 项目中常被忽略的 Bundle ID 管理问题
android·ios·小程序·https·uni-app·iphone·webview
2501_9159214316 小时前
iOS App 测试的工程化实践,多工具协同的一些尝试
android·ios·小程序·https·uni-app·iphone·webview
咸虾米_17 小时前
uniapp+unicloud实战项目,九两酒微信小程序商城及后台管理系统前后端部署运行步骤
微信小程序·uni-app·uniapp实战项目·unicloud云开发·vue3后台管理
怀君17 小时前
Uniapp——Android离线打包之更换启动屏和App图标
uni-app