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

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