import {Component, OnInit, EventEmitter} from '@angular/core'; import {DialogRef, ModalComponent, CloseGuard} from 'ngx-modialog'; import {BSModalContext} from 'ngx-modialog/plugins/bootstrap'; import UtilService from "@common/services/util/index"; declare var $: any; declare const BMap: any; export class BaiduMapContext extends BSModalContext { constructor(private args: any) { super(); /* * 点击背景是否隐藏模态框 * @value true 不隐藏 * @value false 隐藏 * */ this.isBlocking = false; /* * 模态框位置 * @value modal-top 居顶 * */ this.dialogClass = 'modal-dialog modal-top baiduMapModal'; } } @Component({ selector: 'baiduMap', templateUrl: './template.html', styleUrls: ['./style.less'] }) export default class BaiduMap implements OnInit, ModalComponent { data: any = {}; otherData: any = {}; context: any = {}; point: any = {}; overlay: any = {}; private map: any; addressList: any = []; constructor(public dialog: DialogRef, private utilService: UtilService) { this.context = dialog.context; } ngOnInit(): void { this.point = this.context.args.point; setTimeout(() => { this.map = new BMap.Map('locationMap', { enableMapClick: false }); let point = new BMap.Point(117.211663, 39.09494); if (this.point && this.point.lng) { point = new BMap.Point(this.point.lng, this.point.lat); const marker = new BMap.Marker(new BMap.Point(this.point.lng, this.point.lat)); this.overlay = marker; this.map.addOverlay(marker); } this.map.centerAndZoom(point, 16); this.map.enableScrollWheelZoom(); this.map.addEventListener("click", (ev) => { if (this.overlay) { this.map.removeOverlay(this.overlay); } this.point = ev.point; const marker = new BMap.Marker(new BMap.Point(ev.point.lng, ev.point.lat)); this.overlay = marker; this.map.addOverlay(marker); }); }, 500) } selectAddress(value) { this.otherData.showDropdown = false; const myGeo = new BMap.Geocoder(); myGeo.getPoint(value, point => { if (point) { this.map.centerAndZoom(point, 16); this.map.removeOverlay(this.overlay); const overlay = new BMap.Marker(point); this.overlay = overlay; this.map.addOverlay(overlay); this.otherData.keyword = value; this.point = this.overlay.point; } else { this.utilService.toastrError('对不起,没有搜索到结果!'); } }); } searchChange() { if (!this.otherData.keyword) { this.utilService.toastrError('必须输入有效地址'); this.otherData.keyword = ""; return false; } this.otherData.showDropdown = true; const local = new BMap.LocalSearch(this.map, { onSearchComplete: results => { if (local.getStatus() == 0) { this.addressList = []; for (let i = 0; i < results.getCurrentNumPois(); i++) { this.addressList.push(results.getPoi(i).title + ", " + results.getPoi(i).address); } } } }); local.search(this.otherData.keyword); } submit() { this.context.args.callback(this.point); this.dialog.close(); } }