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

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

希望可以帮到你~~

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

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

加个关注不迷路~

相关推荐
用户6990304848758 小时前
try catch使用场景 处理同步代码错误兼容用的
javascript·uni-app
ITKEY_10 小时前
uniapp微信开发者工具 更改AppID失败 touristappid
uni-app
Geek_Vison17 小时前
APP瘦身实战:从80MB+砍到15MB——基于小程序容器技术剥离APP非核心业务的实践分享
小程序·uni-app·mpaas
CHB1 天前
HDC2026 演讲实录|AI 驱动的跨端进化:利用 uni-agent 快速构建高性能鸿蒙应用
uni-app·harmonyos
2501_915918412 天前
iOS App性能测试工具的实现方法与优化循环指南
android·ios·小程序·https·uni-app·iphone·webview
斯内普吖2 天前
(开源)高校素拓分管理系统小程序实战指南 基于 Java + SpringBoot + uni-app + Vue + MySQL
java·spring boot·mysql·小程序·uni-app·开源
海阔天空66882 天前
uniapp开启调试模式
uni-app·uniapp开启调试模式
anyup2 天前
分享 5 套 uni-app 实用主题,一键适配暗黑模式
前端·uni-app·视觉设计
gg159357284603 天前
Uni-app跨平台开发全解课程:从零基础到企业级多端落地实战
vue.js·uni-app
xshirleyl4 天前
uniapp小兔鲜儿day3
uni-app