wr1207
2023-04-17 32e4bad2cff2793052effcd2d59cdd8923702b22
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
<template>
  <div class="app-content">
        <el-dialog title="腾讯地图坐标选择器" width="600px" top="40px" :visible.sync="show" :before-close="closeForm" append-to-body>
            <el-input size="small" @input="getLatLngBounds()" suffix-icon="el-icon-location" v-model="address" autoComplete="off" clearable  placeholder="请输入搜索地址"></el-input>
            <div class="content"  ref="bounds">
                <div v-if="showaddress">
                    <p v-for="(i,index) in BoundsPois" :key="index" @click="selectCity(index,i)">
                        {{i.name}}
                        <span class="address">{{i.address}}</span>
                    </p>
                </div>
                <div id="map"></div>
            </div>
            <div slot="footer" class="dialog-footer">
                <el-button size="small" type="primary" @click="submit" >
                    <span>确 定</span>
                </el-button>
                <el-button size="small" @click="closeForm">取 消</el-button>
            </div>
        </el-dialog>
    </div>
</template>
 
<script>
export default {
  props: {
        //地图key
        mapKey: {
          type: String,
          default: 'KZOBZ-MHMWG-IVJQG-IICXA-E3FFS-TKBZR'
        },
        //周边位置高度
        boundsHeight: {
          type: String,
          default: '200px'
        },
        show: {
            type: Boolean,
            default: false
        },
        address_detail:{
            type:String,
            default:null
        },
    },
    data() {
        let points =  this.address_detail ? JSON.parse(this.address_detail) : {lng:'',lat:'',address:''}
        return {
            lat:points.lat,
            lng:points.lng,
            address: points.address, //搜索关键字
            marker: null, //标记点
            map: null, //地图实例
            setTime: null,
            selectPosition: -1,  //选择的位置
            BoundsPois: [], //周边列表,
            showaddress:true
        };
    },
    mounted() {
        this.loadScript()
    },
    methods: {
        //异步加载地图js
        loadScript() {
            var script = document.createElement("script")
            script.type = "text/javascript";
            window.initMap = () => {
                this.init()
            };
            script.src ="https://map.qq.com/api/js?v=2.exp&key=" +this.mapKey +"&callback=initMap"
            document.body.appendChild(script);
        },
 
        //初始化地图
        init() {
            this.map = new qq.maps.Map(document.getElementById("map"), {
                center: new qq.maps.LatLng(this.lat, this.lng), //设置地图中心的
                keyboardShortcuts: false,  //禁止键盘操控
                disableDefaultUI: true,    //禁止所有控件
                zoom: 16
            });
            if(!this.lat){
                this.getLocation();
            }else{
                let latLng = new qq.maps.LatLng(this.lat, this.lng)
                this.setMarker(latLng);
            }
          
            //绑定地图点击事件
            qq.maps.event.addListener(this.map, "click", e => {
                let latLng = new qq.maps.LatLng(e.latLng.lat, e.latLng.lng)
                this.marker.setPosition(latLng)
                this.marker.setAnimation(qq.maps.MarkerAnimation.DOWN)
                this.getAddress(latLng)
            });
            //绑定地图中心点改变事件
            qq.maps.event.addListener(this.map, "center_changed", e => {
                if(this.selectPosition != -1) return;
                this.centerChanged(e);
            });
            //绑定地图开始拖动事件
            qq.maps.event.addListener(this.map, "dragstart", e => {
                //重置选择、搜索框
                this.address = '';
                this.selectPosition = -1;
            });
            //绑定地图缩放级别更改事件
            qq.maps.event.addListener(this.map, "zoom_changed", e => {
                //重置选择、搜索框
                this.address = '';
                this.selectPosition = -1;
            });
        },
 
        //地图中心点改变事件
        centerChanged(e, type) {
            let center = this.map.getCenter();
            if (this.marker) {
                this.marker.setPosition(center);
                this.marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
            }
        },
 
        //查询位置
        searchCity() {
            let geocoder = new qq.maps.Geocoder({
                complete: result => {
                    this.map.setCenter(result.detail.location);
                    //设置标注点位置
                    this.marker.setPosition(result.detail.location);
                    //设置标注点下落动画
                    this.marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
                }
            });
            geocoder.getLocation(this.address);
        },
 
        //搜索位置,查询周边位置信息
        getLatLngBounds(init) {
            this.selectPosition = -1;
            let latlngBounds = new qq.maps.LatLngBounds();
            //调用Poi检索类
            let searchService = new qq.maps.SearchService({
                complete: results => {
                    this.scrollTop();
                    this.BoundsPois = results.detail.pois;
                    this.map.setCenter(this.BoundsPois[0].latLng);
                    //设置标注点为中心点
                    this.marker.setPosition(this.BoundsPois[0].latLng);
                }
            });
            //周边位置个数10个
            searchService.setPageCapacity(20);
            searchService.searchInBounds(this.address, latlngBounds); //根据范围和关键字进行指定区域检索。
        },
 
        //根据经纬度进行位置解析
        getAddress(latLng) {
            let geocoder = new qq.maps.Geocoder({
                complete: result => {
                    this.scrollTop();
                    //添加当前的经纬度信息
                    let nowPostin = {name: '当前标注位置', address: result.detail.address, latLng: {lat: result.detail.location.lat, lng: result.detail.location.lng}};
                    this.BoundsPois = [nowPostin, ...result.detail.nearPois];
                }
            });
            geocoder.getAddress(latLng || this.map.getCenter());
        },
 
        //选择周边滚动条返回到顶部
        scrollTop () {
            this.$refs.bounds.scrollTop = 0;
        },
 
        //在列表中选择位置
        selectCity (index,row) {
            this.selectPosition = index;
            let lat = this.BoundsPois[index].latLng.lat,
            lng = this.BoundsPois[index].latLng.lng,
            latLng = new qq.maps.LatLng(lat, lng);
            //将地图中心移至当前坐标
            this.map.panTo(latLng);
            //设置标注点位置
            this.marker.setPosition(latLng);
            //设置标注点下落动画
            this.marker.setAnimation(qq.maps.MarkerAnimation.DOWN);
 
            this.address = row.address + row.name
            this.lat = lat
            this.lng = lng
            this.showaddress = false
        },
 
        //获取当前位置
        getLocation() {
            let cs = new qq.maps.CityService({
                map: this.map,
                complete: results => {
                    this.latLng = [results.detail.latLng.lat, results.detail.latLng.lng];
                    this.map.setCenter(results.detail.latLng);
                    this.setMarker(results.detail.latLng);
                }
            });
            cs.searchLocalCity();
        },
 
        //设置底部标记点
        setMarker(center) {
            let icon = new qq.maps.MarkerImage(
            "data:image/png;base64,iVBORw0KGgoAAAANSUhEUgAAADQAAABJCAMAAABFGvXGAAACf1BMVEUAAAD/PyL/PyL/PyL/PyL/PyL/PyL/PyL/PyL/PyL/PyL/PyL/PyL/PyP/PyL/PyL+PiL/PyL/PyL/PyP/PyL/PyL/PyL/PyL/PyL4OyP/PiL/PyL/PyL/PyLzOiX5PCX9QCj/PyLQJiX0RkTkNC39PyXsNST/PyL/PyL/PyLyRUTgNDPdMjHzRELxQ0HzRUPTJyTZKyf1RULeLyf0Qz30Qzz8PST/PyLWLS3rPz/RKCjMJCTZLy/mOjnUKirPJSXLIiLzRkXpPDvWLCvgNDP0R0XsPz3ZLyzgNDPVKijRJSTtPzvcMS71R0TjNjPZLCnyREHpOzjiNDDtPzvkNS7kNTDsPDXYKiPiLyPqNy3zQTncLCTpMyTxOinsNif0QTf1Qzz5QjTxOyr4QC/tQT/mOjnwQkDmOTfPJSLdLyr0RULyQj3nOTbWKibTKCT0RD/yQTrxQz/lNCzbLSjyPzX2RkHXKSL2RDzgMSjkMiXiMCP4RDrrNyn/////XVv/S0v/W1n/SEf/YF7/UVD/VVP/WFb/REP/TUz/YV//Xlz/Pj7/T07/RkX/QED/Ozv/Skn/WVj/OTn/VlX/U1L/QkL/QUH/ODf5UlH4UU/vQUD7V1b2TEv0SUj/+vr3Tk3/2dn9XVz8W1r6TEv7R0b7REPvPTzzPDvoPDvnOjnpODjnMDDjLS3dKir/vr7/qqn/pqX/mZn/ZWT6VVP9VFP//Pz/8PD/4eH/3t3/zs7/w8P/ra3/iIj/fX3/e3r5UU/+TUzsPTz5PDz+N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uTrneeuszMR9R7qMqeF9qghPcpvxdJxEGRa1qVTjVLbOctocFCdxW5H/Qo30YmqyUaKVbUTlKUUqh3TLbMo3SraaVH1AkepxQiOulaNFmyolp6oN/iJGeDaQUZ9fpyTVbXInXg2hlW1M9XFZqsc0F74Iw2e1p0+VnFPVZ67Vy//6CxLtYzvXHZahborLw/po4tFlvMNrpJ2a4d3wLoied51+Xg9pqcdM67/tBgNa2I5vz0vwdqWrMf8IkpbG5BlccyEtNYOznTaThnCSYe34eCItH/OtQkRQogz28W/SOW/GuxrbDUJExIAt+r08KuJlv3k5TcBwYC+MLTd/EEsfOrs9LY1gF8CAjG3ke9J5PxKfH4YcDpymnb0I52SvOdYCeH7EYa2tPwmo75g2IniaPM3epTrvmmWLz484rNlXUqUrM6QigmF1qD/zn/oO0hHBsPq//ue87p/lhBFJhjW94Z/U0NltxhHJK8mwRn1OOJ9HkVdcJqxdvV8FMK96zyGuuGxY6xo4qQFHBK+4QlifAoFPyhGRYQUCyhGRYa348kVVRDCsAQOyiFukHJZ+2/Qcsx5GpDwg6/WyYDg1Axpycw1wOGUJURrwv1OtJq38BbsNQs0bptLnAAAAAElFTkSuQmCC",
                new qq.maps.Size(52, 73),
                new qq.maps.Point(0, 0),
                new qq.maps.Point(12, 34),
                new qq.maps.Size(45, 65)
            );
            this.marker = new qq.maps.Marker({
                map: this.map,
                position: center
            });
            this.marker.setIcon(icon);
        },
        submit(){
            let address = {address:this.address,lng:this.lng,lat:this.lat}
            this.$emit('update:address_detail', JSON.stringify(address))
            this.closeForm()
        },
        closeForm(){
            this.$emit('update:show', false)
        }
    }
};
</script>
<style lang="scss" scoped>
.content {
      width: 100%;
      background: rgba(252, 250, 250, 0.918);
      border: 1px solid #f1f1f1;
      border-top: none;
      font-size: 13px;
      color: #5a5a5a;
      max-height: 400px;
      overflow-y: auto;
      p {
        text-overflow: ellipsis;
        white-space: nowrap;
        overflow: hidden;
        padding: 8px 10px;
        margin: 0;
        cursor: pointer;
        &:hover {
          background: #eff6fd;
        }
        .address {
          font-size: 12px;
          color: #b9b9b9;
          margin-left: 20px;
        }
      }
}
#map {
  height: 400px;
}
</style>