修改小程序无法更改定位的问题

This commit is contained in:
赵毅 2025-09-29 16:38:57 +08:00
parent e13b06ece0
commit f5d3cd79de

View File

@ -2,35 +2,19 @@
<view class="city-select-page" v-if="loading">
<view class="white_container padding_bottom40">
<!-- 搜索框 -->
<u-search
placeholder="输入城市进行搜索"
:value="searchValue"
@search="handleSearch"
@change="handleSearch"
:showAction="false"
height="70"
searchIconSize="40"
shape="round"
></u-search>
<u-search placeholder="输入城市进行搜索" :value="searchValue" @search="handleSearch" @change="handleSearch"
:showAction="false" height="70" searchIconSize="40" shape="round"></u-search>
<view v-if="searchValue" class="search_popup">
<view v-if="searchRes.length === 0" class="list-item">暂无搜索结果</view>
<view
v-else
v-for="(item, index) in searchRes"
:key="index"
:class="['list-item', index === searchRes.length - 1 && 'no_border']"
@click="headerSelectMapClick(item)"
>
<view v-else v-for="(item, index) in searchRes" :key="index"
:class="['list-item', index === searchRes.length - 1 && 'no_border']" @click="headerSelectMapClick(item)">
{{ item.name }}
</view>
</view>
<view v-if="!searchValue">
<!-- 当前定位城市 -->
<view class="current-city">
<image
src="https://wechat-img-file.oss-cn-beijing.aliyuncs.com/Index_add.png"
mode="widthFix"
/>
<image src="https://wechat-img-file.oss-cn-beijing.aliyuncs.com/Index_add.png" mode="widthFix" />
<text>当前定位城市 {{ location.cityName }}</text>
</view>
@ -38,13 +22,8 @@
<view class="hot-cities">
<text class="title">国内热门城市</text>
<view class="city-list">
<view
v-for="(item, index) in hotCityList"
class="city_name"
:key="index"
@click="headerSelectMapClick(item)"
>{{ item.name }}</view
>
<view v-for="(item, index) in hotCityList" class="city_name" :key="index"
@click="headerSelectMapClick(item)">{{ item.name }}</view>
</view>
</view>
</view>
@ -52,28 +31,15 @@
<view v-if="!searchValue" class="container">
<!-- 左侧列表 -->
<scroll-view
class="list-scroll"
scroll-y
:scroll-into-view="activeId"
@scroll="handleScroll"
>
<view
v-for="(group, index) in groupedData"
:key="index"
class="white_container"
:id="'group-' + group.letter"
>
<scroll-view class="list-scroll" scroll-y :scroll-into-view="activeId" @scroll="handleScroll">
<view v-for="(group, index) in groupedData" :key="index" class="white_container" :id="'group-' + group.letter">
<!-- 字母标题 -->
<view class="letter-title">{{ group.letter }}</view>
<!-- 列表项 -->
<!-- TODO: 小程序编译避坑指南 -->
<view
v-for="(item, ind) in group.list"
:key="item.id"
<view v-for="(item, ind) in group.list" :key="item.id"
:class="['list-item', ind === group.list.length - 1 && 'no_border']"
@click="() => { headerSelectMapClick(item) }"
>
@click="() => { headerSelectMapClick(item) }">
{{ item.name }}
</view>
</view>
@ -81,12 +47,7 @@
<!-- 右侧索引栏 -->
<view class="index-bar">
<view
v-for="(letter, index) in letters"
:key="index"
class="index-item"
@tap="scrollToLetter(letter)"
>
<view v-for="(letter, index) in letters" :key="index" class="index-item" @tap="scrollToLetter(letter)">
{{ letter }}
</view>
</view>
@ -96,7 +57,7 @@
<script>
import { apiArr } from "../../api/area.js";
import { request, debounce } from "../../utils/index.js";
import { request, debounce, NavgateTo } from "../../utils/index.js";
export default {
data() {
@ -182,14 +143,38 @@ export default {
return;
}
//
//
let cityName = '';
let district = '';
let region = '';
//
const cityMatch = res.address.match(/(.*?(?:省|自治区)|^)(.*?)(?:市|地区|盟|州|县|区)?$/);
if (cityMatch && cityMatch[2]) {
const cityMatch2 = cityMatch[2].match(/(.*市)/);
if (cityMatch2 && cityMatch2[1]) {
cityName = cityMatch2[1];
}
}
//
const districtMatch = res.address.match(/市(.*)/);
if (districtMatch && districtMatch[1]) {
district = districtMatch[1];
}
//
const regionMatch = res.address.match(/^(.*?省.*?(?:市|自治州|盟).*?(?:县|区|旗))/);
if (regionMatch && regionMatch[1]) {
region = regionMatch[1];
}
const selectLocation = {
cityName: res.address
.match(/(.*?(?:省|自治区)|^)(.*?)(?:市|地区|盟|州|县|区)?$/)[2]
.match(/(.*市)/)[1],
district: res.address.match(/市(.*)/)[1],
cityName: cityName,
district: district,
lat: res.latitude,
lng: res.longitude,
region: res.address.match(/^(.*?省.*?(?:市|自治州|盟).*?(?:县|区|旗))/)[1], //
region: region,
};
uni.setStorageSync("location", selectLocation); //
// uni.setStorageSync('city', selectLocation.cityName); //