pyt
2025-02-24 b31c7c629a2efddc53c89b8f0df0de0e2a33e8d9
1
2
3
4
5
6
7
8
9
10
11
12
13
14
15
16
17
18
19
20
21
22
23
24
25
26
27
28
29
30
31
32
33
34
35
36
37
38
39
40
41
42
43
44
45
46
47
48
49
50
51
52
53
54
55
56
57
58
59
60
61
62
63
64
65
66
67
68
69
70
71
72
73
74
75
76
77
78
79
80
81
82
83
84
85
86
87
88
89
90
91
92
93
94
95
96
97
98
99
100
101
102
103
104
105
106
107
108
109
110
111
112
113
114
115
116
117
118
119
120
121
122
123
124
125
126
127
128
129
130
131
132
133
134
135
136
137
138
139
140
141
142
143
144
145
146
147
148
149
150
151
152
153
154
155
156
157
158
159
160
161
162
163
164
165
166
167
168
169
170
171
172
173
174
175
176
177
178
179
180
181
182
183
184
185
186
187
188
189
190
191
192
193
194
195
196
197
198
199
200
<template>
    <view class="content">
        <u-navbar title="选择地点" bgColor="transparent" titleStyle="font-size: 35rpx;font-weight:bold;" placeholder>
            <view slot="left" @click="back">
                <image src="/static/location/back.png" class="w-19 h-35" mode=""></image>
            </view>
        </u-navbar>
        <view class="" id="container" @regionchange="onRegionChange">
 
        </view>
        <view class=" main">
            <view class="searchMain  mb-35 mt-38">
                <view class="search fs-27 ">
                    <text class="mr-35 ml-50">成都市</text>
                    <image class="w-17 h-8 mr-35" src="/static/location/toleft.png" mode=""></image>
                    <image class="w-31 h-31 mr-13" src="/static/location/search.png" mode=""></image>
                    <input class="flex1" v-model="keyword" placeholder="搜索小区/写字楼等"
                        placeholder-style="font-size:27rpx"></input>
                    <view class="sure" @click="searchLocation">
                        确定
                    </view>
                </view>
 
            </view>
            <view class="card" @click="changeAdr(item.id,item)" :class="item.id ==uid ?'activecard':''"
                v-for="item in locationList" :key="item.id">
                <text>{{item.address}}</text>
                <text class="font-w500 fs-23 mt-25" style="color: rgba(0, 0, 0, 0.6);">{{item.address}}</text>
 
            </view>
        </view>
 
    </view>
</template>
 
<script>
    export default {
        data() {
            return {
                latitude: 39.909,
                longitude: 116.39742,
                map: null,
                uid: 0,
                keyword: '',
                locationList: [],
            };
        },
        methods: {
            back() {
                uni.navigateBack()
            },
            onRegionChange(e) {
                if (e.type === 'end') {
                    // 地图移动结束时执行操作
                    console.log('地图移动结束,新的中心点经度:', e.longitude, '新的中心点纬度:', e.latitude);
                }
            },
            changeAdr(value, addr) {
                console.log(value, addr, 'asdasdada');
                this.uid = value
                const pages = getCurrentPages()
                let prevPage = pages[pages.length - 2];
                prevPage.location = addr.address
                setTimeout(() => {
                    uni.navigateBack()
                }, 500)
            },
            // 搜索地点
            searchLocation() {
                if (!this.keyword) return;
                uni.request({
                    url: `https://api.map.baidu.com/place/v2/search`,
                    method: 'GET',
                    data: {
                        query: this.keyword,
                        location: `${30.39},${104.04}`,
                        radius: 10000, // 搜索半径,单位:米
                        output: 'json',
                        ak: '3mHKIXMArjgIkgADzOlTYp4XssNSNkwr' // 替换为你自己的 AK
                    },
                    success: (res) => {
                        console.log(res, '-------');
                        if (res.data.status === 0) {
                            this.searchResults = res.data.results;
                            this.latitude = res.data.results[0].location.lat
                            this.longitude = res.data.results[0].location.lng
                            this.locationList = res.data.results.map((result, index) => {
 
                                result.id = index + 1
 
                                return result
                            });
                        }
                    },
                    fail: (err) => {
                        console.error('搜索失败:', err);
                    }
                });
            },
        },
        onLoad() {
 
        },
        onReady() {
            this.map = new BMapGL.Map("container"); // 创建地图实例 
        
            uni.getLocation({
                type: 'wgs84',
                success: (res) => {
                    console.log(res, 'res');
                    this.longitude = res.longitude;
                    this.latitude = res.latitude;
                    var point = new BMapGL.Point(res.longitude, res.latitude); // 创建点坐标
                    this.map.centerAndZoom(point, 12);
                    var myGeo = new BMapGL.Geocoder();
                    // 根据坐标得到地址描述    
                    myGeo.getLocation(new BMapGL.Point(res.longitude, res.latitude), (result) => {
                        console.log(result, 'result');
                        this.locationList = result.surroundingPois.map((item, index) => {
                            item.id = index + 1
                            return item
                        })
 
                    })
                }
            });
        }
    }
</script>
 
<style lang="scss" scoped>
    #container {
        width: 100vw;
        height: 534rpx;
    }
 
    .content {
        display: flex;
        flex-direction: column;
        align-items: center;
 
        .main {
            background-color: #fff;
            border-radius: 34rpx;
            position: absolute;
            top: 517rpx;
            z-index: 100;
 
            .activecard {
                background: #F2F2F2;
            }
 
        }
 
    }
 
 
    .searchMain {
        padding-left: 40rpx;
        padding-right: 40rpx;
        border-radius: 40rpx;
 
        .search {
            width: 669rpx;
            height: 77rpx;
            background: #FFFFFF;
            border-radius: 38rpx;
            border: 2rpx solid #DADADA;
            display: flex;
 
            align-items: center;
            justify-content: space-between;
        }
    }
 
 
    .sure {
        width: 104rpx;
        height: 69rpx;
        background: linear-gradient(270deg, #FC8D55 0%, #FF4948 100%);
        border-radius: 48rpx;
        font-weight: 500;
        font-size: 23rpx;
        color: #FFFFFF;
        display: flex;
        align-items: center;
        justify-content: center;
    }
 
    .card {
        display: flex;
        flex-direction: column;
        font-weight: 800;
        font-size: 31rpx;
        color: rgba(0, 0, 0, 0.8);
        padding: 34rpx 38rpx 33rpx 38rpx;
        background: #fff;
        border-bottom: 2rpx solid rgba(0, 10, 26, 0.07);
    }
</style>