app-map-position.vue 12.6 KB
Newer Older
1 2
<template>
    <div class="app-map-position">
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
        <el-input
            size="small"
            @focus="handleMapShow"
            v-model="value"
            :placeholder="placeholder ? placeholder : $t('components.appMapPosition.title')">
        </el-input>
        <el-dialog
            :title="$t('components.appMapPosition.title')"
            class="map-modal"
            :visible.sync="dialogShow">
            <div class="search-toolbar">
                <el-input id="map__search" size="small" @change="handleSearch" v-model="searchAddress" />
                <div id="map__result" class="content-result" v-show="resultShow"></div>
            </div>
            <div class="map__content">
                <el-amap
                    :center="center" 
                    :amap-manager="amapManager"
                    zoom="12"
                    :events="events"
                    ref="map">
                    <el-amap-marker
                        class="map-marker"
                        vid="component-marker"
                        :position="marker.position">
                        <div>
                            <img src="//a.amap.com/jsapi_demos/static/demo-center/icons/poi-marker-default.png">
                            <span class="input-map__marker">{{ marker.address }}</span>
                        </div>
                    </el-amap-marker>
                </el-amap>
                
            </div>
            <template slot="footer">
                <el-button
                    type="primary"
                    size="small"
                    @click="handleSubmit">
                    {{ $t('components.appMapPosition.submit') }}
                </el-button>
            </template>
        </el-dialog>
45 46 47 48
    </div>
</template>

<script lang='tsx'>
49
import { Vue, Component, Prop, Watch, Model } from 'vue-property-decorator';
50
import { Subject, Subscription } from 'rxjs';
51
import { AMapManager } from 'vue-amap';
52

53
@Component({})
54
export default class AppMapPosition extends Vue  {
55 56 57 58 59 60 61 62
    
    /**
     * 双向绑定表单项值
     *
     * @type {*}
     * @memberof AppMapPosition
     */  
    @Model('change') public value: any;
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
     * 占位内容
     *
     * @type {*}
     * @memberof AppMapPosition
     */ 
    @Prop() public placeholder?: string;

    /**
     * 经度
     *
     * @type {*}
     * @memberof AppMapPosition
     */ 
    @Prop() public longitude?: any;

    /**
     * 纬度
     *
     * @type {*}
     * @memberof AppMapPosition
     */ 
    @Prop() public latitude?: any;

    /**
     * 地址
     *
     * @type {*}
     * @memberof AppMapPosition
     */ 
    @Prop() public address?: any;

    /**
     * 模式(address、longitude、latitude)
     *
     * @type {*}
     * @memberof AppMapPosition
     */ 
    @Prop({default: 'address'}) public mode: any;

    /**
     * 表单数据
     *
     * @type {*}
     * @memberof AppMapPosition
109
     */
110
    @Prop() public data: any;
111 112

    /**
113 114 115 116
     * 表单通讯对象
     *
     * @type {*}
     * @memberof AppMapPosition
117
     */
118
    @Prop() public formState!: Subject<any>;
119 120

    /**
121
     * 搜索框显示值
122
     *
123 124
     * @type {*}
     * @memberof AppMapPosition
125
     */
126
    public searchAddress: string = '';
127 128

    /**
129 130 131
     * AMap SDK对象
     *
     * @type {*}
132 133
     * @memberof AppMapPosition
     */
134
    public amapManager: any = new AMapManager();
135 136

    /**
137 138 139
     * 地图中心点
     *
     * @type {*}
140 141
     * @memberof AppMapPosition
     */
142
    public center: any[] = [104.09427199999999, 30.660396];
143 144

    /**
145 146 147
     * 地图模态框显示状态
     *
     * @type {*}
148 149
     * @memberof AppMapPosition
     */
150
    public dialogShow: boolean = false;
151 152

    /**
153 154 155
     * 地图标点信息
     *
     * @type {*}
156 157
     * @memberof AppMapPosition
     */
158
    public marker: any = {};
159 160

    /**
161 162 163
     * 初始化地图标点
     *
     * @type {*}
164 165
     * @memberof AppMapPosition
     */
166 167 168 169
    public markerResult: any = {};

    /**
     * 事件集合
170
     *
171
     * @type {*}
172 173
     * @memberof AppMapPosition
     */
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
    public events: any = {};

    /**
     * 获取地址需求AMap插件对象
     *
     * @type {*}
     * @memberof AppMapPosition
     */
    public geocoder: any;

    /**
     * 当前 window
     *
     * @type {*}
     * @memberof AppMapPosition
     */
    public win: any;

    /**
     * 搜索结果显示框状态
     *
     * @type {*}
     * @memberof AppMapPosition
     */
    public resultShow: boolean = false;

    /**
     * Vue生命周期
     *
     * @memberof AppMapPosition
     */
    public created() {
        this.win = window as any;
        if(this.formState) {
            this.formState.subscribe(({ type, data }) => {
                if(Object.is('load', type)) {
                    this.initMap();
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
    }

    /**
     * Vue生命周期
     *
     * @memberof AppMapPosition
     */
    public mounted() {
        let amap: any = this.win.AMap;
        amap.plugin(["AMap.Geocoder"], () => {
            this.geocoder = new amap.Geocoder({
                extensions: "all",
            })
        })
        this.initMapEvents();
    }

    /**
     * 根据当前模式初始化地图
     *
     * @memberof AppMapPosition
     */
    public initMap() {
237
        if(!this.mode) {
238 239 240 241 242 243 244 245 246 247 248 249 250 251 252 253 254
            return;
        }
        if(Object.is(this.mode, 'address')) {
            this.initByAddress()
        } else if(Object.is(this.mode, 'longitude')) {
            this.initByLng();
        } else if(Object.is(this.mode, 'latitude')) {
            this.initByLat();
        }
    }

    /**
     * mode = address,初始化地图
     *
     * @memberof AppMapPosition
     */
    public initByAddress() {
255
        if(this.longitude && this.latitude && this.data && this.value) {
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
            const position = [this.data[this.longitude], this.data[this.latitude]];
            Object.assign(this.marker, {
                position: position,
                address: this.value,
                visible: true
            });
            this.center = position;
            this.searchAddress = this.marker.address;
            Object.assign(this.markerResult, this.marker);
        } else {
            Object.assign(this.marker, {
                position: [104.09427199999999, 30.660396],
                address: '四川省成都市成华区猛追湾街道四川电视塔天府熊猫塔',
                visible: true
            })
            this.searchAddress = this.marker.address;
        }
    }

    /**
     * mode = longitude,初始化地图
     *
     * @memberof AppMapPosition
     */
    public initByLng() {
281
        if(this.latitude && this.data && this.value) {
282 283 284 285 286 287 288 289 290 291
            this.handleMarker(this.value, this.data[this.latitude], this);
        } else {
            Object.assign(this.marker, {
                position: [104.09427199999999, 30.660396],
                address: '四川省成都市成华区猛追湾街道四川电视塔天府熊猫塔',
                visible: true
            })
            this.searchAddress = this.marker.address;
        }
    }
292 293

    /**
294
     * mode = latitude,初始化地图
295 296 297
     *
     * @memberof AppMapPosition
     */
298
    public initByLat() {
299
        if(this.longitude && this.data && this.value) {
300 301 302 303 304 305 306 307
            this.handleMarker(this.data[this.longitude], this.value, this);
        } else {
           Object.assign(this.marker, {
                position: [104.09427199999999, 30.660396],
                address: '四川省成都市成华区猛追湾街道四川电视塔天府熊猫塔',
                visible: true
            })
            this.searchAddress = this.marker.address;
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
     * 初始化地图事件
     *
     * @memberof AppMapPosition
     */
    public initMapEvents() {
        const that: any = this;
        that.events = {
            click($event: any) {
                that.mapClick($event);
            },
            init($event: any) {
                that.map = $event;
            }
        };
    }

    /**
     * 展开模态框
     *
     * @memberof AppMapPosition
     */
    public handleMapShow() {
        this.resultShow = false;
        this.dialogShow = true;
336 337 338
        if(!this.markerResult || JSON.stringify(this.markerResult) == "{}") {
            return;
        }
339 340 341 342 343 344 345
        this.searchAddress = this.markerResult.address;
        Object.assign(this.marker, this.markerResult);
        this.center = this.markerResult.position;
    }
    
    /**
     * 处理地图标点
346
     * 
347 348 349 350
     * @param {*} lng 经度
     * @param {*} lat 纬度
     * @param {*} that this指针
     * @param {boolean} flag 是否更新结果集
351 352
     * @memberof AppMapPosition
     */
353 354 355 356 357 358 359 360 361 362 363 364
    public async handleMarker(lng: any, lat: any, that: any, flag: boolean = true) {
        const address = await this.getAddress(lng, lat).catch((error) => {
            console.warn(error);
        });
        if(!address) {
            return;
        }
        Object.assign(that.marker, { position: [lng, lat], address: address, visible: true });
        that.searchAddress = address;
        that.center = [lng, lat];
        if(flag) {
            Object.assign(this.markerResult, this.marker);
365 366
        }
    }
367 368 369 370 371 372 373 374 375 376 377 378 379 380 381 382 383 384 385 386 387 388 389 390 391 392 393 394 395 396 397 398 399 400 401 402 403 404 405 406 407 408 409 410 411 412 413 414 415 416 417 418 419 420 421 422 423 424 425 426 427 428 429 430 431 432 433 434 435 436 437 438 439 440 441 442 443 444 445 446 447 448 449 450 451 452 453 454 455 456 457 458 459 460 461 462 463

    /**
     * 搜索地址
     * 
     * @memberof AppMapPosition
     */
    public handleSearch() {
        this.center = [];
        const that = this;
        let placeSearch: any;
        //  调用服务搜索结果
        that.win.AMap.service(["AMap.PlaceSearch"], () => {
            placeSearch = new this.win.AMap.PlaceSearch({
                pageSize: 5,
                city: '成都',
                citylimit: false,
                panel: 'map__result',
            })
            placeSearch.search(that.searchAddress, (status: any, result: any) => {
                if (status == 'complete' && result.info == 'OK') {
                    this.resultShow = true;
                    if(result.poiList.pois) {
                        that.handleMarker(result.poiList.pois[0].location.R, result.poiList.pois[0].location.Q, that, false);
                    }
                }
            })
        })
        //  监听搜索结果列表点击事件
        that.win.AMap.event.addListener(placeSearch,"listElementClick", (e: any) => {
            if(e.data.location) {
                that.handleMarker(e.data.location.R, e.data.location.Q, that, false);
            }
        })
    }

    /**
     * 地图点击事件
     * 
     * @memberof AppMapPosition
     */
    public mapClick($event: any) {
        if(!$event && !$event.lnglat) {
            return;
        }
        const that = this;
        that.handleMarker($event.lnglat.lng, $event.lnglat.lat, that, false);
    }

    /**
     * 调用服务,根据经纬度获取地址信息
     * 
     * @param {*} lng 经度
     * @param {*} lat 纬度
     * @memberof AppMapPosition
     */
    public getAddress(lng: any, lat: any) {
        return new Promise((resolve, reject) => {
            this.geocoder.getAddress([lng,lat],(status:any,result: any) => {
                if (status === 'complete' && result.info === 'OK') {
                    if (result && result.regeocode) {
                        const address = result.regeocode.formattedAddress;
                        resolve(address);
                    }
                }
            })
        })
    }

    /**
     * 点击模态确认按钮,提交数据
     * 
     * @memberof AppMapPosition
     */
    public handleSubmit() {
        this.dialogShow = false;
        Object.assign(this.markerResult, this.marker);
        if(!this.markerResult || !this.mode) {
            return;
        }
        if(Object.is(this.mode, 'address')) {
            this.$emit('change', this.markerResult.address);
            const lng = { name: this.longitude, value: this.markerResult.position[0].toString() };
            const lat = { name: this.latitude, value: this.markerResult.position[1].toString() };
            this.$emit('itemChange', lng, lat);
        } else if(Object.is(this.mode, 'longitude')) {
            this.$emit('change', this.markerResult.position[0]);
            const address = { name: this.address, value: this.markerResult.address };
            const lat = { name: this.latitude, value: this.markerResult.position[1].toString() };
            this.$emit('itemChange', address, lat);
        } else if(Object.is(this.mode, 'latitude')) {
            this.$emit('change', this.markerResult.position[1]);
            const address = { name: this.address, value: this.markerResult.address };
            const lng = { name: this.longitude, value: this.markerResult.position[0].toString() };
            this.$emit('itemChange', address, lng);
        }
    }

464 465 466
}
</script>

467 468
<style lang='scss'>
@import './app-map-position.scss';
469
</style>