当前位置:网站首页>租房小程序自动定位城市
租房小程序自动定位城市
2022-08-02 17:16:00 【詹Sir(开源字节)】
微信小程序提供的只有获取到经纬度的接口没有直接获取城市的接口
方案一
小程序调用wx.getLocation()获取经纬度,然后调用高德的接口获取当前城市,代码如下:
// findLocation(){ // uni.showLoading({title:"定位中....",mask:true}) // uni.getLocation({ // type: 'gcj02', // success: async res => { // let { longitude, latitude } = res; // let result = await wxGetAddress({ longitude, latitude }); // let province = result.regeocodeData.addressComponent.province // let cityName = result.regeocodeData.addressComponent.city // this.locationCity = cityName // let lifeData = uni.getStorageSync('lifeData'); // let vuex_city = lifeData.vuex_city // if(!vuex_city){ // this.$u.vuex('vuex_city', cityName); // } // uni.hideLoading(); // } // }); // } // import amapFile from '../../libs/amap-wx.130.js' // const myAmapFun = new amapFile.AMapWX({ // // 申请的高德key值 // key: '4ef3b3b42d8bb823529908dc93414127' // }); //创建一个实例化对象 // export default function wxGetAddress({longitude,latitude}) { // //根据传递进来经纬度进行反解析,调用的是高德给的方法 // return new Promise((resolve, reject) => { // myAmapFun.getRegeo({ // location: `${longitude},${latitude}`, // success: (res) => { // resolve(res[0]) // }, // fail: (err) => { // resolve(null) // } // }) // }) // }
方案二
微信调用wx.getLocation()获取经纬度,审核过于麻烦,审核了很多次都不给通过,果断放弃。因此开源字节的租房小程序使用ip进行城市定位。代码如下:
findLocation(){ let that = this uni.showLoading({title:"定位中....",mask:true}) this.$u.get("/api/profile/getRealCity").then(obj => { let cityName = obj.msg if(cityName){ this.locationCity = cityName let lifeData = uni.getStorageSync('lifeData'); let vuex_city = lifeData.vuex_city if(!vuex_city){ this.$u.vuex('vuex_city', cityName); } uni.hideLoading(); }else{ uni.request({ // url:'https://pv.sohu.com/cityjson?ie=utf-8', url:'https://tianqiapi.com/ip?version=v9&appid=23035354&appsecret=8YvlPNrz', success(resp) { let ip = resp.data.ip that.$u.get("/api/profile/getRealCityByIP?ip="+ip).then(obj => { let cityName = obj.msg if(cityName){ that.locationCity = cityName let lifeData = uni.getStorageSync('lifeData'); let vuex_city = lifeData.vuex_city if(!vuex_city){ that.$u.vuex('vuex_city', cityName); } uni.hideLoading(); }else{ that.$mytip.toast('定位失败') } uni.hideLoading(); }); } }) } }); }
通过方案二即可满足定位需求,需注意以下几个点:
1、我们的nginx被微信代理后无法获取到真实ip,因此使用第三方接口获取ip
2、微信小程序只能使用备案的域名,我们需要将获取ip的接口添加到白名单中
3、获取到ip后,再请求后台地址得到城市信息

以上代码在我们的开源项目中都能获取到,感兴趣的小伙伴可以前往我们的开源仓库
如若转载,请注明出处:开源字节 https://sourcebyte.cn/article/207.html
边栏推荐
- 融云「 IM 进阶实战高手课」系列直播上线
- LeetCode·76.最小覆盖子串·滑动窗口
- 【genius_platform软件平台开发】第七十五讲:YUY2转RGB24实现源码
- NoSQL之redis缓存雪崩、穿透、击穿概念解决办法
- golang源码分析(5):sync.Once
- Oracle 11 g rac finished patch, dbca new patches of SQL database also needs to perform?
- 什么是实时流引擎?
- FP6606CLP5 SOP-8 USB Type-C和PD充电控制器
- Continuous integration (4) Jenkins configuration alarm mechanism
- SQL语句基础
猜你喜欢
再获权威认证!马上消费安逸花APP通过中国信通院“金融APP人脸识别安全能力评测”
故障分析 | 一条 SELECT 语句跑崩了 MySQL ,怎么回事?
【Redis】连接报错:Could not connect to Redis at 127.0.0.1:6379: Connection refused
DeepMind 首席科学家 Oriol Vinyals 最新访谈:通用 AI 的未来是强交互式元学习
【genius_platform软件平台开发】第七十五讲:YUY2转RGB24实现源码
Continuous integration (4) Jenkins configuration alarm mechanism
navicat premium 15 下载安装详细教程
Pytest study notes
Redis的介绍和使用
白话电子签章原理及风险
随机推荐
ffmpeg cannot find libx264 after compilation
Nacos配置中心工作原理(超简单)
蔚来杯2022牛客暑期多校训练营5 ABCDFGHK
【300+精选大厂面试题持续分享】大数据运维尖刀面试题专栏(十)
嵌入式Qt-做一个秒表
「全球数字经济大会」登陆 N 世界,融云提供通信云服务支持
图解LeetCode——622. 设计循环队列(难度:中等)
LeetCode·每日一题·
FP6606CLP5 SOP-8 USB Type-C和PD充电控制器
golang刷leetcode动态规划(8)盈利计划
What is an APS system?What should I pay attention to when importing APS?Worth watching again and again
npm install 编译时报“Cannot read properties of null (reading ‘pickAlgorithm‘)“
9月起中国给予多哥等16国98%税目产品零关税待遇
nacos简单使用
golang源码分析(4):select
LeetCode·76.最小覆盖子串·滑动窗口
mui中使用多级选择器实现省市区联动
Navicat for mysql cracked versions installed
golang学习之七:并发编程基础(goroutine、channel、select)
MYSQL一站式学习,看完即学完