uni-app中uni.getLocation获取不到address,但有经纬度信息
uni-app中uni.getLocation获取不到address,但有经纬度信息
uni.getLocation({
    type: 'wgs84',
    geocode: true,
    success: function(res) {
        console.log(JSON.stringify(res))
        // 返回信息
        {"type":"wgs84","latitude":34.77048925012855,"longitude":113.72570191216609,"speed":0,"accuracy":67,"errMsg":"getLocation:ok"}
    }
});
更多关于uni-app中uni.getLocation获取不到address,但有经纬度信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html
HBuilderX 升级到2.0以上版本 详情 http://www.dcloud.io/hbuilderx.html
manifest 开启自定义组件模式 详情 https://ask.dcloud.net.cn/article/35843
需要参数 geocode 详情 https://uniapp.dcloud.io/api/location/location?id=getlocation
注意平台差异:目前仅App平台支持
更多关于uni-app中uni.getLocation获取不到address,但有经纬度信息的实战教程也可以访问 https://www.itying.com/category-93-b0.html
有个屁用,之前获取的到,今天突然获取不到了
回复 baijuyi: 兄弟搞定了么 获取到没?
没用,之前还可以的,代码没改,重新发包之后,就获取address了
我也是,之前做的时候好好的,现在没法获取address了,代码什么都没动,就升级过hbuilder,目前3.3.11
geocode为true,安卓需指定 type 为 gcj02 并配置三方定位SDK
嗯,改为gcj02,目前安卓好了。好奇iOS下用哪个。目前还没做iOS的
这是什么诡异bug,改了之后获取到了
我这边iOS调试的时候,将type设置为gcj02也获取不到address
我也是
回复 碎月无痕: 兄弟解决了没啊,{“type”:“gcj02”,“altitude”:0,“latitude”:29.85596761067708,“longitude”:121.5884933810764,“speed”:0,“accuracy”:4178.230089724466,“errMsg”:“getLocation:ok”}我依旧拿不到,安卓有时候可以拿到,iOS一直拿不到。权限啥的都配置了
获取不到address,目前仅App平台支持,其他平台需要如何处理呢??有没有什么好的解决方案。
Android,真机调试可以,打包后获取不到
在uni-app中,uni.getLocation API 默认只能获取到设备的经纬度信息(如纬度 latitude 和经度 longitude),而不会直接返回地址信息(如 address)。要获取详细的地址信息,你需要结合地理编码服务(Geocoding)来实现,这通常是通过调用第三方地图服务API(如高德地图、百度地图等)来完成的。
以下是一个使用高德地图API结合uni-app获取地址信息的示例代码:
- 
首先,确保你已经在高德地图开放平台申请了一个API Key。
 - 
在uni-app项目中安装axios(用于HTTP请求):
npm install axios - 
在页面中编写获取地址信息的代码:
 
<template>
  <view>
    <button @click="getLocation">获取地址</button>
    <view v-if="address">地址:{{ address }}</view>
  </view>
</template>
<script>
import axios from 'axios';
export default {
  data() {
    return {
      address: '',
      key: '你的高德地图API Key', // 替换为你的高德地图API Key
    };
  },
  methods: {
    async getLocation() {
      try {
        const { latitude, longitude } = await uni.getLocation({ type: 'gcj02' });
        const { data } = await axios.get(`https://restapi.amap.com/v3/geocode/regeo?output=json&location=${longitude},${latitude}&key=${this.key}`);
        if (data.regeocode && data.regeocode.formattedAddress) {
          this.address = data.regeocode.formattedAddress;
        } else {
          uni.showToast({ title: '获取地址失败', icon: 'none' });
        }
      } catch (error) {
        console.error('获取位置信息失败', error);
        uni.showToast({ title: '获取位置信息失败', icon: 'none' });
      }
    },
  },
};
</script>
<style scoped>
/* 添加你的样式 */
</style>
注意事项:
- 确保在调用 
uni.getLocation时指定type: 'gcj02',因为高德地图API使用的是国测局坐标(GCJ-02)。 axios.get请求的URL中,location参数需要按照经度,纬度的格式拼接。- 高德地图API有请求频率限制,请根据实际使用场景合理规划请求。
 - 为了安全起见,不要在客户端代码中硬编码API Key,可以考虑通过服务器端获取或环境变量管理。
 
通过上述步骤,你可以在uni-app中结合高德地图API实现根据经纬度获取详细地址的功能。
        
      
                    
                  
                    

