uni-app 获取当前位置的经纬度以及地址信息

文章目录

uni-app 获取当前位置的经纬度以及地址信息

uni.getLocation(objc)

uni-app官方文档定位API: uni.getLocation(OBJECT)

js 复制代码
uni.getLocation({
	type: 'wgs84',
	success: function (res) {
		console.log('当前位置的经度:' + res.longitude);
		console.log('当前位置的纬度:' + res.latitude);
	}
});

获取经纬度和地址

  1. 去高德开放平台注册账号,添加应用,申请个AppKey

  2. 设置

1)配置勾选系统定位和高德定位,配置高德的"用户名"和"app key"

2)添加定位权限

<uses-permission android:name="android.permission.LOCATION"/>

  1. 上代码
js 复制代码
<template>
  <view class="container">
    <text class="title">定位示例</text>
    <button @click="getLocation">获取经纬度</button>
    <view v-if="location">
      <text>纬度: {{ location.latitude }}</text>
      <text>经度: {{ location.longitude }}</text>
      <text>速度: {{ location.speed }} m/s</text>
      <text>精度: {{ location.accuracy }} m</text>
    </view>
	<view v-if="address">
		<text>位置: {{ address.formatted_address }} </text>
	</view>
    <view v-if="error">
      <text>错误信息: {{ error.message }}</text>
    </view>
  </view>
</template>

<script>
export default {
  data() {
    return {
      location: null,
	  address: null,
      error: null
    };
  },
  methods: {
    getLocation() {
      uni.getLocation({
        type: 'wgs84',
        success: (res) => {
          this.location = {
            latitude: res.latitude,
            longitude: res.longitude,
            speed: res.speed,
            accuracy: res.accuracy,
          };
		  this.reverseGeocode(res.latitude,res.longitude);
          console.log('定位成功:', res);
        },
        fail: (err) => {
          this.error = err;
          console.error('定位失败:', err);
        }
      });
    },
	// 逆地理编码函数
	reverseGeocode(latitude, longitude) {
	  const key = '46XXXXXXXXXXXXXXXXXXXXXX0a'; // 换成你自己的AppKey
	  const url = `https://restapi.amap.com/v3/geocode/regeo?key=${key}&location=${longitude},${latitude}`;
	 
	  uni.request({
	    url: url,
	    method: 'GET',
	    success: (res) => {
	      if (res.statusCode === 200 && res.data.status === '1') {
			  this.address = {
			    formatted_address: res.data.regeocode.formatted_address,
			  };
	        console.log(res.data.regeocode);
	      } else {
	        console.error('逆地理编码失败:', res.data);
	      }
	    },
	    fail: (error) => {
	      console.error('请求失败:', error);
	    }
	  });
	}
  },
  onLoad() {
    this.getLocation();
  }
};
</script>

<style>
.container {
  padding: 20px;
}

.title {
  font-size: 20px;
  font-weight: bold;
  margin-bottom: 20px;
}

button {
  margin-bottom: 20px;
}

text {
  display: block;
  margin-bottom: 10px;
}
</style>

1)先调用uni.getLocation(objc) 函数可以拿到经纬度信息

2)再调用高德地图你地理编码的API获取经纬度对应的地址信息。

调试结果

在Safari上可以获取到经纬度和位置信息:

在Chrome上调试获取不到;

打包apk手机上也可以获取到经纬度和位置信息。

问题

1)uni.getLocation(objc) 这个函数有一个参数geocode

App上支持这个方法直接获取地址信息,需要设置 geocode 位 true,同时type参数需要设置成gcj02。

js 复制代码
    getLocation() {
      uni.getLocation({
        type: 'gcj02',
		geocode: true,
        success: (res) => {
          this.location = {
            latitude: res.latitude,
            longitude: res.longitude,
            speed: res.speed,
            accuracy: res.accuracy,
			address: res.address,
          };
          console.log('定位成功:', res);
        },
        fail: (err) => {
          this.error = err;
          console.error('定位失败:', err);
        }
      });
    },

我试了试,手机打包可以获取到经纬度,但是位置信息没拿到,不知道是哪里的配置问题,示例如下:

相关推荐
2501_915909063 小时前
手机崩溃日志导出的工程化体系,从系统级诊断到应用行为分析的多工具协同方法
android·ios·智能手机·小程序·uni-app·iphone·webview
郑州光合科技余经理4 小时前
技术视角:海外版一站式同城生活服务平台源码解析
java·开发语言·uni-app·php·排序算法·objective-c·生活
wangdaoyin20107 小时前
UniApp中使用LivePlayer进行视频或在流媒体播放
uni-app·liveplayer·h5播放视频
2501_915106327 小时前
App HTTPS 抓包实战解析,从代理调试到真实网络流量观察的完整抓包思路
网络协议·http·ios·小程序·https·uni-app·iphone
游戏开发爱好者88 小时前
苹果App Store应用程序上架方式全面指南
android·小程序·https·uni-app·iphone·webview
2501_916008898 小时前
深入理解 iPhone 文件管理,从沙盒结构到开发调试的多工具协同实践
android·ios·小程序·https·uni-app·iphone·webview
一室易安9 小时前
解决使用 UniApp 搭配 Vue3 小程序开始 使用uview-plus 的返回顶部up-back-top中onPageScroll 不触发的问题
小程序·uni-app
yilan_n9 小时前
鸿蒙应用上传
vue.js·华为·uni-app
yilan_n9 小时前
【UniApp实战】手撸面包屑导航与路由管理 (拒绝页面闪烁)
前端·javascript·vue.js·uni-app·gitcode
一室易安9 小时前
uniapp+vue3 微信小程序中 页面切换tab 页面滚动到指定锚点位置,滚动页面时候到达指定锚点位置吸顶tab 会自动进行切换
微信小程序·uni-app·notepad++