当前位置: 首页 > news >正文

小程序 地理位置授权怎么搞

用uniapp开发小程序,获取位置信息-地理位置授权,怎么做

大概实现样子:

真机

代码如下:

<template><view class="choose-store-wrap"><view class="search-section"><view class="city-selector" @click="selectCity"><text class="city-name">{{ selectedCity }}</text><text class="arrow">▼</text></view><view class="search-box"><text class="search-icon">🔍</text><input class="search-input" type="text" placeholder="搜索你要选择的门店"v-model="searchKeyword"@input="handleSearch"/></view></view><view class="store-list"><view class="store-item" v-for="(store, index) in filteredStores" :key="store.id"@click="selectStore(store)"><view class="store-header"><text class="store-name">{{ store.outShopName }}</text><view class="distance-info"><text class="distance-icon">📍</text><text class="distance">{{ store.lat }}</text></view></view><view class="store-tag" v-if="index === 0">距您最近</view><view class="store-address">{{ store.address }}</view><!-- <view class="store-room">{{ store.termNo }}</view> --></view></view></view>
</template><script>
export default {data() {return {selectedCity: '绿藤市',searchKeyword: '',stores: [// {// 	id: 1,// 	outShopName: '绿藤绿地新都汇店',// 	address: '绿藤市时光里广场时光里广场时光里广场',// 	lat: '距您304m'// }]}},computed: {filteredStores() {if (!this.searchKeyword) {return this.stores}return this.stores.filter(store => store.outShopName.includes(this.searchKeyword) || store.address.includes(this.searchKeyword))}},onLoad() {this.getNearbyStores()// 页面加载时请求位置权限this.requestLocationPermission()},methods: {goBack() {uni.navigateBack()},selectCity() {// 跳转到城市选择页面uni.navigateTo({url: '/pages/citySelect/citySelect'})},handleSearch() {// 搜索逻辑已在computed中处理},selectStore(store) {// 选择门店后返回上一页const pages = getCurrentPages()const prevPage = pages[pages.length - 2]if (prevPage) {// 将选中的门店信息传递给上一页prevPage.$vm.selectedStore = store}uni.navigateBack()},requestLocationPermission() {// 使用微信原生位置授权APIuni.getSetting({success: (res) => {console.log('获取设置成功:', res)if (res.authSetting['scope.userLocation']) {// 已经授权,直接获取位置this.getUserLocation()} else if (res.authSetting['scope.userLocation'] === false) {// 用户之前拒绝过,引导用户手动开启this.showLocationGuide()} else {// 首次请求位置权限this.requestLocationAuth()}},fail: (err) => {console.error('获取设置失败:', err)// 如果获取设置失败,直接尝试获取位置this.getUserLocation()}})},requestLocationAuth() {// 请求位置权限uni.authorize({scope: 'scope.userLocation',success: () => {console.log('位置权限授权成功')this.getUserLocation()},fail: (err) => {console.log('位置权限授权失败:', err)if (err.errMsg.includes('auth deny')) {// 用户拒绝授权this.showLocationGuide()} else {uni.showToast({title: '位置权限获取失败',icon: 'none'})}}})},getUserLocation() {// 获取用户位置uni.getLocation({type: 'gcj02',success: (res) => {console.log('位置获取成功:', res)// 根据位置获取附近门店this.getNearbyStores(res.latitude, res.longitude)},fail: (err) => {console.error('位置获取失败:', err)if (err.errMsg.includes('auth deny')) {this.showLocationGuide()} else {uni.showToast({title: '位置获取失败,请手动选择城市',icon: 'none'})}}})},showLocationGuide() {// 引导用户手动开启位置权限uni.showModal({title: '位置权限',content: '需要获取您的位置信息来匹配附近门店,请在设置中开启位置权限',confirmText: '去设置',cancelText: '手动选择',success: (res) => {if (res.confirm) {// 跳转到设置页面uni.openSetting({success: (settingRes) => {if (settingRes.authSetting['scope.userLocation']) {// 用户开启了位置权限this.getUserLocation()}}})} else {// 用户选择手动选择城市uni.showToast({title: '请手动选择城市',icon: 'none'})}}})},getNearbyStores(lat, lng) {// 调用API获取附近门店// =============获取门店接口============console.log('获取附近门店', lat, lng)uni.request({url:'https://xxx/app/info/shop/shopList',method:'GET',data:{},success: (res) => {console.log('门店res',res);this.stores = res.data.data},fail: (err) => {console.error('获取门店列表失败:', err)uni.showToast({title: '获取门店列表失败',icon: 'none'})}})}}
}
</script><style>
.choose-store-wrap {min-height: 100vh;background: #f5f5f5;
}.header {background: #fff;/* padding-top: 44px; */
}.nav-bar {display: flex;align-items: center;justify-content: space-between;padding: 12px 16px;height: 44px;
}.back-btn {width: 44px;height: 44px;display: flex;align-items: center;justify-content: center;
}.back-icon {font-size: 24px;color: #333;
}.title {font-size: 18px;font-weight: 600;color: #333;
}.right-icons {display: flex;gap: 12px;width: 44px;justify-content: flex-end;
}.icon {font-size: 16px;color: #666;
}.search-section {display: flex;padding: 12px 16px;background: #fff;gap: 12px;
}.city-selector {display: flex;align-items: center;gap: 4px;padding: 8px 12px;background: #f5f5f5;border-radius: 6px;min-width: 80px;
}.city-name {font-size: 14px;color: #333;
}.arrow {font-size: 12px;color: #666;
}.search-box {flex: 1;display: flex;align-items: center;background: #f5f5f5;border-radius: 6px;padding: 8px 12px;gap: 8px;
}.search-icon {font-size: 16px;color: #999;
}.search-input {flex: 1;font-size: 14px;color: #333;
}.store-list {padding: 12px 16px;
}.store-item {background: #fff;border-radius: 8px;padding: 16px;margin-bottom: 12px;position: relative;
}.store-header {display: flex;justify-content: space-between;align-items: flex-start;margin-bottom: 8px;
}.store-name {font-size: 16px;font-weight: 600;color: #333;flex: 1;
}.distance-info {display: flex;align-items: center;gap: 4px;
}.distance-icon {font-size: 12px;
}.distance {font-size: 12px;color: #666;
}.store-tag {position: absolute;top: 16px;right: 16px;background: #e7f4f2;color: #61aea7;font-size: 12px;padding: 2px 8px;border-radius: 4px;
}.store-address {font-size: 14px;color: #666;line-height: 1.4;margin-bottom: 4px;
}.store-room {font-size: 14px;color: #999;
}
</style>

注意,会报这个:

记得在manifest.json中配置:

http://www.dtcms.com/a/474654.html

相关文章:

  • 福州贸易公司网站制作seo的网站
  • 开网站 主机 服务器百度竞价广告的位置
  • 【Unity每日一记】Unity脚本教程:用脚本创建物体与输入系统详解
  • 动易网站官网使用python建设一个论坛网站
  • Java-集合练习3
  • 2.c++面向对象(六)
  • STM32 环境监测项目笔记(一):DHT11 温湿度传感器原理与驱动实现
  • C++ 完全背包
  • 【Linux】理解链接过程
  • 广州做网站多少钱怎么做简单的网站首页
  • 【机器人学中的状态估计】7.5.2习题证明:(Cu)^=(2cos(phi)+1)u^-u^C-C^Tu^公式证明
  • Flask、Nginx 与 Docker 的分工与协作
  • 怎么建立一个公司的网站吗ui界面设计作品模板
  • 网站浮动广告怎么做qq开放平台网站开发申请不通过的原因
  • redis中的list命令
  • 对网站建设课程的心得体会北京旅游网页设计
  • 碎片化知识整理利器:NoteGen——AI驱动的免费开源笔记工具使用指南
  • 网站的建设方法包括什么问题高端网站建设大概多少费用
  • RabbitMQ Exchange类型与绑定规则详解
  • 太平洋建设官方网站wordpress 显示分类
  • 比特币私钥位数范围动态估计源代码
  • 随机游走:从布朗运动到PageRank算法的数学之旅
  • 机器学习周报十七
  • DeepCode:从论文到完整软件开发的全自动AI工具
  • 深入探索现代前端开发:从基础到架构的完整指南
  • Sora2高级玩法:超越基础生成的创意新世界(FL去水印送邀请码)
  • 自己怎样优化网站wordpress博客位置
  • 大型购物网站服务器h5页面制作工具易企秀
  • ESP32 + Arduino IDE 开发的 MQTT 通信程序
  • 网站策划哪里找WordPress访问确认