xufenglei
2018-07-20 efa7b6d79650d2f6813071b49f02ccce94c03f86
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
import { NzModalSubject } from 'ng-zorro-antd';
import { ReactiveFormsModule } from '@angular/forms';
import { ViewEncapsulation, Component,  ViewChild, ElementRef, NgZone, OnInit } from '@angular/core';
import { MapOptions, Point, MarkerOptions, ControlAnchor, NavigationControlOptions, NavigationControlType, BMapInstance } from 'angular2-baidu-map';
import { CoorPicker } from '@business/entity/data';
import { CoorPickerService } from 'app/routes/map/coordinates-picker/coordinates-picker.service';
 
 
 
@Component({
  encapsulation: ViewEncapsulation.None,
  selector: 'app-coordinates-picker',
  templateUrl: './coordinates-picker.component.html',
  styleUrls: [ './coordinates-picker.component.css' ],
})
export class CoordinatesPickerComponent implements OnInit {
    Default_LNG = 121;
    Default_LAT = 31.4;
    showZoom = 19;
    isSaving = false;
    markerOption: {
      point: Point,
      options: MarkerOptions
    } = {
      point: null,
      options: null
    };
    data: CoorPicker;
    navigationOpts: NavigationControlOptions;
    options: MapOptions;
    _BMap: any = null;
 
  constructor(private subject: NzModalSubject, private coorPickerService: CoorPickerService) {
   
  }
  ngOnInit(): void {
    this.data = this.coorPickerService.data;
    let lng = this.data.longitude;
    lng = !lng ? this.Default_LNG : lng;
    this.data.longitude = lng;
    let lat = this.data.latitude;
    lat = !lat ? this.Default_LAT : lat;
    this.data.latitude = lat;
    this.options = {
        minZoom: 3,
        maxZoom: 19,
        cursor: 'default',
        centerAndZoom: { 
          lng: this.data.longitude, 
          lat: this.data.latitude,
          zoom: this.showZoom
        },
        enableKeyboard: true
      };  
      this.navigationOpts = {
        anchor: ControlAnchor.BMAP_ANCHOR_BOTTOM_RIGHT,
        type: NavigationControlType.BMAP_NAVIGATION_CONTROL_LARGE
      };
      this.markerOption.point = {
        lng: lng,
        lat: lat
      };
      this.markerOption.options = {
        icon: {
          imageUrl: './assets/img/map_coordinates.png',
          size: {
            height: 30,
            width: 30
          }
        },
        offset: {
           height: -30,
           width: -15
        }
       };
  }
  private _marker: any = null;
  loadMarker(marker) {
    if (this._marker == null) {
      this._marker = marker;      
    }
  }
  private _map: any;
  private _localSearch;
  loadMap(map: any) {      
      this._map = map;
      this._BMap = window.BMap;  
      this._map.addEventListener(
        'tilesloaded',
        (type, fn) => {
          //  this._map.clearOverlays();
           this._map.addOverlay(this._marker);          
        }
      ); 
      this._localSearch = new  this._BMap.LocalSearch(map, {
        renderOptions: {map: map}
      });
      if (this.data.latitude === this.Default_LAT 
          && this.data.longitude === this.Default_LNG
          && !!this.data.address
          && !!this.data.address.trim()) {
          this.mapSearch(this.data.address);
      }
  }
  city: string;
  // queryText: string;
   mapSearch(queryText: string, isSearch?: boolean) {
  
      if (!!queryText && !!queryText.trim()) {
        this.city = !!this.city ? this.city : '北京市';
        const end = queryText.indexOf('市') + 1;
        if (end > 0) {
          let start = queryText.indexOf('省') + 1;
          start = start > 0 ? start : queryText.indexOf('区') + 1;
          start = start > end ? 0 : start;
          if (start < end) {
             this.city = queryText.slice(start, end).trim();
             if (!!this.city) {
                this._map.setCurrentCity(this.city); 
             }
          }
        }
        console.log(this.city);
        const myGeo = new this._BMap.Geocoder();
        const that = this;
        that._map.setCurrentCity(that.city);
        myGeo.getPoint(queryText, function(point){
            let getPoint = false;
            if (point) {
              that.data.latitude = point.lat;
              that.data.longitude = point.lng;
              getPoint = true;
            }else {
                console.log('您选择地址没有解析到结果!');
            }
            that.markerOption.point = {
              lng:  that.data.longitude,
              lat:  that.data.latitude
            };
            that.options.centerAndZoom = { 
                lng: that.data.longitude, 
                lat: that.data.latitude,
                zoom: that.showZoom
            };
            // setTimeout(() => {
            if (getPoint && !isSearch) {
              that._map.centerAndZoom(point, that.showZoom);
              
            } else {
              that._localSearch.search(queryText);              
            }
            // }, 3000);
        }, that.city);
      }
  }
  clickMap(e: any) {
       this.markerOption.point = {
         lng: e.point.lng,
         lat: e.point.lat
       };
       this.coorPickerService.data.longitude = e.point.lng;
       this.coorPickerService.data.latitude = e.point.lat;
  }
  close() {
      this.subject.destroy();
  }
  save() {
      this.isSaving = true;
      this.subject.next();
      this.subject.destroy();
  }
}