陶杰
2024-12-23 d39644872fa6f9499fc2c1651bced631a9e96b19
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
201
202
203
204
205
206
207
208
209
210
211
212
213
214
215
216
217
218
219
220
221
222
223
224
225
226
227
228
229
230
231
232
233
234
235
236
237
238
239
240
241
242
243
244
245
246
247
248
249
250
251
252
253
254
255
256
257
258
259
260
261
262
263
264
265
266
267
268
269
270
271
272
273
274
275
276
277
278
279
280
281
282
283
284
285
286
287
288
289
290
291
292
293
294
295
296
297
298
299
300
301
302
303
304
305
306
307
308
309
310
311
312
313
314
315
316
317
318
319
320
321
322
323
324
325
326
327
328
329
330
331
332
333
334
335
336
337
338
339
340
341
342
343
344
345
346
347
348
349
350
351
352
353
354
355
356
357
358
359
<template>
    <view class="content">
        <!--     <view class="btns">
            <view @click="back">取 消</view>
            <view @click="getCurrentLocation">获取当前地址</view>
        </view> -->
        <view class="inputCon">
            <view class="searchView">
                <text class="iconfont icon-sousuo"  @click="searchFn"></text>
                <input type="text" placeholder="搜索地点" v-model="searchWords" confirm-type="search" @confirm="searchFn" />
                <text @click="cancel">取消</text>
            </view>
        </view>
        <!-- 地图部分 -->
        <view class="content-map">
            <map style="width: 100%;height: 100%;" v-if="mapFlafg" :latitude="latitude" :longitude="longitude" :markers="markers"
                @tap="tap"  :scale="16"  :title="title"/>
        </view>
        <!-- <view id="container"></view> -->
        <!--  搜索框 -->
 
        <!--  地点列表部分 -->
        <view class="list">
            <view class="item" v-for="(add,index) in dataTips" :key="add.id" @click="select(add,index)"
                :class="selectIndex==index?'active':''">
                <view class="name">{{add.name}}</view>
                <view class="address">{{add.district || ''}}{{add.address || ''}}</view>
            </view>
        </view>
    </view>
</template>
 
<script>
    // 引入高德地图api提供的微信小程序的接口
    import amapFile from '@/map/amap-wx.130.js';
    // 创建地图
    var myAmapFun = new amapFile.AMapWX({
        key: '75d7da3feb329cd5ae997975b953abb6'
    }); //key值要申请为 去高德地图申请微信小程序的key
    // var myAmapFun = new amapFile.AMapWX({key: ''}); //key我的
    export default {
        data() {
            return {
                mapFlafg:false,
                selectIndex: undefined,
                selectAddr: {},
                searchWords: "",
                id: 1, // 使用 marker点击事件 需要填写id
                title: 'map',
                latitude: 39.91667, // 纬度
                longitude: 116.41667, // 经度
 
                markers: [{
                    latitude: 39.91667, // 纬度
                    longitude: 116.41667, // 经度
                    width: 30,
                    height: 30,
                    iconPath: ''
                    // iconPath: '../../static/ditu.png'
                }],
                dataTips: []
            }
        },
        onLoad() {
            var self = this;
            uni.getLocation({
                type: 'gcj02',
                success: function(res) {
                    console.log(res, '当前地址定位');
                    if (res.errMsg == "getLocation:ok") {
                        console.log(self.mark, 'onload里面看看');
                        self.longitude = res.longitude;
                        self.latitude = res.latitude;
                        self.$set(self.markers[0],"longitude",res.longitude);
                        self.$set(self.markers[0],"latitude",res.latitude);
                        self.mapFlafg=true;
                        console.log(self.markers,"markers")
                        // self.markers[0].longitude = res.longitude;
                        // self.markers[0].latitude = res.latitude;
                    }
                },
                complete: () => {
                    // 获取当前位置的地点列表
                    myAmapFun.getPoiAround({
                        location: self.longitude + ',' + self.latitude,
                        success: (data) => {
                            console.log("获取当前的列表", data);
                            this.dataTips = data.poisData;
                        },
                        fail: (info) => {
                            console.log(info, '点击地图错误信息1')
                        }
                    })
                }
            });
            
        },
        methods: {
            cancel() {
                if (this.searchWords) {
                    this.searchWords = "";
                    myAmapFun.getPoiAround({
                        location: this.markers[0].longitude + ',' + this.markers[0].latitude,
                        success: (data) => {
                            console.log("获取当前的列表", data);
                            this.$nextTick(() => {
                                this.dataTips = data.poisData;
                            })
                        },
                        fail: (info) => {
                            console.log(info)
                        }
                    })
                }
            },
            reserGeo() {
                myAmapFun.getRegeo({
                    success: (data) => {
                        console.log("getRegeo", data);
                    },
                    fail: (info) => {
                        console.log(info)
                    }
                })
            },
            // 根据地址列表中选择某一个地点
            select(add, index) {
                if (!add) {
                    return;
                }
                this.selectIndex = index;
                var location = add.location.split(",");
                console.log(location, add, '列表地点的经纬度');
                this.selectAddr = {
                    address: add.pname ? (add.pname + add.cityname + add.adname) : (add.district + add
                        .address),
                    detailAddress: add.address,
                    latitude: location[1],
                    longitude: location[0]
                };
                this.markers[0].latitude = +location[1];
                this.markers[0].longitude = +location[0];
                uni.setStorageSync("address", this.selectAddr.address);
                // 选择地点后,将选取的地点传递到前一个页面中
                // var pages = getCurrentPages(); // 获取所有的页面栈
                // var prevPage = pages[pages.length - 2]; // 找到上一个页面,注意是页面,如果是页面中有组件,则需要通过页面接受到数据后,再次往组件中传递
                // prevPage.$vm.userAddress.locationAddress = this.selectAddr.address; //在上一个页面中就可以用userAddress进行接收
                // prevPage.$vm.userAddress.detail = this.selectAddr.detailAddress; //在上一个页面中就可以用userAddress进行接收
                // prevPage.$vm.selectAddr = this.selectAddr;
                // prevPage.$vm.address = {
                //     province: add.pname,
                //     city: add.cityname,
                //     district: add.adname,
                // }
                uni.navigateBack({
                    delta: 1
                });
 
            },
            // 在地图上点击进行选点,这个选点在地图缩放比例较大时无效,因为精读的问题。
            tap(e) {
                console.log(e, '点击了地图');
                var location = e.detail.longitude + ',' + e.detail.latitude
                myAmapFun.getRegeo({
                    location: location,
                    success: (data) => {
                        console.log("获取指定定位信息", data);
                        this.selectAddr = {
                            address: data[0].regeocodeData.formatted_address,
                            latitude: e.detail.latitude,
                            longitude: e.detail.longitude
                        };
                        this.markers[0].latitude = data[0].latitude;
                        this.markers[0].longitude = data[0].longitude;
                        myAmapFun.getPoiAround({
                            location: data[0].longitude + ',' + data[0].latitude,
                            success: (data) => {
                                console.log("获取当前的列表", data);
                                this.dataTips = data.poisData;
                            },
                            fail: (info) => {
                                console.log(info, '点击地图错误信息1')
                            }
                        })
                    },
                    fail: (info) => {
                        console.log(info, '点击地图错误信息2');
                    }
                })
            },
            // 根据内容进行检索
            searchFn() {
                console.log("根据地址检索", this.searchWords);
                myAmapFun.getInputtips({
                    keywords: this.searchWords,
                    location: '',
                    success: (data) => {
                        console.log(111, data);
                        if (data && data.tips) {
                            this.dataTips = data.tips;
                        }
                    },
                    fail: data => {
                        console.log(222, data);
                    }
                })
            },
            // getCurrentLocation() {
            //     let that = this
            //     uni.getLocation({
            //         type: 'wgs84',
            //         success: function(res) {
            //             console.log(res, '当前地址定位');
            //             if (res.errMsg == "getLocation:ok") {
            //                 console.log(that.mark, 'onload里面看看');
            //                 this.longitude = res.longitude;
            //                 this.latitude = res.latitude;
            //                 this.markers[0].longitude = res.longitude;
            //                 this.markers[0].latitude = res.latitude;
            //             }
            //         }
            //     });
            // },
        }
    }
</script>
 
<style lang="scss" scoped>
    .btns {
 
        position: fixed;
        top: 0;
        left: 0;
        height: 260upx;
        width: 100%;
        background: linear-gradient(to bottom, rgba(0, 0, 0, 0.4), rgba(0, 0, 0, 0));
        display: flex;
        align-items: center;
        justify-content: space-between;
        z-index: 10 !important;
 
        view {
            border-radius: 10%;
            margin: 100upx 24upx 0;
            font-size: 30upx;
 
            &:first-child {
                color: #fff;
            }
 
            &:last-child {
                width: 100upx;
                height: 60upx;
                line-height: 60upx;
                text-align: center;
                border-radius: 10upx;
                background: #20c295;
                color: #fff;
            }
        }
    }
 
    .content {
        // position: relative;
        height: 100vh;
        display: flex;
        flex-direction: column;
 
        .content-map {
            width: 100%;
            height: 50vh;
        }
 
        .list {
            // flex: 0 1 calc(50vh - 10upx);
            border-radius: 30rpx;
            background-color: #fff;
            // position: absolute;
            // bottom:-660rpx;
            height: calc(50vh - 10upx);
            overflow-y: auto;
            width: 100%;
            margin: 0upx auto 0;
            padding-bottom: 20upx;
 
            .item {
                border-bottom: 2upx solid #f3f3f3;
                padding: 0 30rpx;
 
                &:last-child {
                    border: none;
                }
 
                .address {
                    font-size: 22upx;
                    color: #666;
                    margin: 10upx 0;
                }
 
                .name {
                    font-size: 30upx;
                    color: #333;
                    margin-top: 10upx;
                }
 
                &.active {
                    .name {
                        font-weight: bold;
                        color: #E13500;
                    }
 
                    .address {
                        color: #E13500;
                    }
                }
            }
        }
 
        .inputCon {
            flex: 0 1 108rpx;
            width: 100%;
            background: #fff;
            // top: -60upx;
            // position: relative;
            z-index: 20;
            // height: 100upx;
            display: flex;
            align-items: center;
            justify-content: center;
 
            .searchView {
                width: 702upx;
                height: 60upx;
                display: flex;
                align-items: center;
                line-height: 60upx;
                border-radius: 40upx;
                padding: 0 30upx;
                box-sizing: border-box;
                background: #f3f3f3;
                font-size: 26upx;
 
                .iconfont {
                    color: #666;
                    margin-right: 20upx;
                }
 
                input {
                    flex: 1;
                }
 
                view {
                    flex-shrink: 0;
                }
            }
        }
 
    }
</style>