123456789101112131415161718192021222324252627282930313233343536373839404142434445464748495051525354555657585960616263646566676869707172737475767778798081828384858687888990919293949596979899100101102103104105106107108109110111112113114115116117118119120121122123124125126127128129130131132133134135136137138139140141142143144145146147148149150151152153154155156157158159160161162163164165166167168169 |
- // const key = 'INCBZ-QYVK3-72F3B-RMOA2-OOIZ2-63BSR'; // 使用在腾讯位置服务申请的key
- // const referer = '红孩儿测试'; // 调用插件的app的名称
- // const hotCitys = ['北京', '上海', '广州', '深圳']; // 用户自定义的的热门城市
- // const accurate = '1'; // 是否使用getLocation进行定位
- // "permission": {
- // "scope.userLocation": {
- // "desc": "你的位置信息将用于小程序定位"
- // }
- // },
- // "plugins": {
- // "citySelector": {
- // "version": "1.0.3",
- // "provider": "wx63ffb7b7894e99ae"
- // }
- // },
- Page({
- data: {
- latitude: 0, // 当前纬度
- longitude: 0, // 当前经度
- nearbyAddresses: [
- {
- name: '附近地址 1',
- address: '详细地址 1',
- checked: false
- },
- {
- name: '附近地址 2',
- address: '详细地址 2',
- checked: false
- }
- ], // 附近地址列表
- selectedAddress: null // 用户选中的地址
- },
- onShow() {
- // wx.navigateTo({
- // url: `plugin://citySelector/index?key=${key}&referer=${referer}&hotCitys=${hotCitys}&accurate=${accurate}`,
- // });
- },
-
- onLoad() {
- this.checkLocationAuthorization();
- },
- // 检查位置授权
- checkLocationAuthorization() {
- wx.getSetting({
- success: (res) => {
- if (!res.authSetting['scope.userLocation']) {
- // 用户未授权,请求授权
- wx.authorize({
- scope: 'scope.userLocation',
- success: () => {
- this.getLocation();
- },
- fail: () => {
- // 用户拒绝授权,提示用户手动开启位置服务
- wx.showModal({
- title: '提示',
- content: '请在设置中开启位置服务授权',
- showCancel: false,
- confirmText: '去设置',
- success: (res) => {
- if (res.confirm) {
- wx.openSetting({});
- }
- }
- });
- }
- });
- } else {
- // 用户已授权,直接获取位置信息
- this.getLocation();
- }
- }
- });
- },
- // 获取当前位置
- getLocation() {
- wx.getLocation({
- type: 'wgs84',
- success: (res) => {
- const latitude = res.latitude;
- const longitude = res.longitude;
- this.setData({ latitude, longitude });
- this.getNearbyAddresses(latitude, longitude);
- },
- fail: (err) => {
- console.error('Failed to get location:', err);
- wx.showToast({
- title: '获取位置失败',
- icon: 'none'
- });
- }
- });
- },
- // 获取附近地址
- getNearbyAddresses(latitude, longitude) {
- const apiKey = 'INCBZ-QYVK3-72F3B-RMOA2-OOIZ2-63BSR'; // 替换为你的 API Key
- const url = `https://apis.map.qq.com/ws/geocoder/v1/?location=${latitude},${longitude}&key=${apiKey}`;
- wx.request({
- url,
- success: (res) => {
- if (res.data.status === 0) {
- const address = res.data.result.address;
- const nearbyAddresses = [
- {
- name: '当前位置',
- address: address,
- checked: false
- },
- {
- name: '附近地址 1',
- address: '详细地址 1',
- checked: false
- },
- {
- name: '附近地址 2',
- address: '详细地址 2',
- checked: false
- }
- ];
- this.setData({ nearbyAddresses });
- } else {
- console.error('Failed to get nearby addresses:', res.data.message);
- }
- },
- fail: (err) => {
- console.error('Request failed:', err);
- }
- });
- },
- // 选择地址
- selectAddress(e) {
- const index = e.currentTarget.dataset.index;
- const nearbyAddresses = this.data.nearbyAddresses.map((item, i) => ({
- ...item,
- checked: i === index
- }));
- const selectedAddress = nearbyAddresses[index];
- this.setData({ nearbyAddresses, selectedAddress });
- },
- // 取消
- onCancel() {
- wx.navigateBack();
- },
- // 确认
- onConfirm() {
- const { selectedAddress } = this.data;
- if (selectedAddress) {
- const pages = getCurrentPages();
- const prevPage = pages[pages.length - 2]; // 上一页
- prevPage.setData({ selectedAddress }); // 将选中的地址传递给上一页
- wx.navigateBack();
- } else {
- wx.showToast({
- title: '请选择一个地址',
- icon: 'none'
- });
- }
- }
- });
|