fengxiang
2018-08-14 5913516ca6ac3a15a629803a2bf78ab9a2afcc68
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
import { Component, OnInit } from '@angular/core';
import { _HttpClient } from '@delon/theme';
import { MapOptions, HeatmapData, HeatmapOptions, NavigationControlOptions, ControlAnchor, NavigationControlType } from 'angular2-baidu-map';
 
@Component({
  selector: 'app-grid-trace',
  templateUrl: './grid-trace.component.html',
})
export class GridTraceComponent implements OnInit {
    options: MapOptions;
    private _map: any;
    constructor(
        private http: _HttpClient
    ) { 
        this.options = {
            minZoom: 3,
            maxZoom: 20,
            cursor: 'default',
            centerAndZoom: { 
              lng: 121, 
              lat: 31.4,
              zoom: 15
            },
            enableKeyboard: true
          };  
    }
    loadMap(map: any) {      
        this._map = map;
        this.showGrid();
        this._map.addEventListener(
            'tilesloaded', () => {
                this.showGrid();
            }
        )
    }
    navigationOpts: NavigationControlOptions;
    //热力图
    heatData: HeatmapData = {
        data: [
            {"lng":121,"lat":31.4,"count":99},
            {"lng":120.99,"lat":31.399,"count":95},
            {"lng":121.002,"lat":31.392,"count":80},
        ],
        max: 100
    };
    heatOption: HeatmapOptions = {
        radius: 100
    };
    heatmapOverlay :any;
    /**
     * loadHeatmap
     */
    public loadHeatmap(heatmapOverlay) {
        this.heatmapOverlay = heatmapOverlay;
    }
    ngOnInit() {
        this.navigationOpts = {
            anchor: ControlAnchor.BMAP_ANCHOR_BOTTOM_RIGHT,
            type: NavigationControlType.BMAP_NAVIGATION_CONTROL_LARGE
          };
    }
    //map为地图对象
    // TODO 临时
    bs() {
        debugger;
        var bs = this._map.getBounds();  //获取当前地图范围的经纬度
        // var bssw = bs.getSouthWest();        //获取西南角的经纬度(左下端点)
        // var bsne = bs.getNorthEast();        //获取东北角的经纬度(右上端点)
        if(!this.topLeftAnchor){
            this.topLeftAnchor = {x0:0,y0:0};
            this.topLeftAnchor.x0 =  bs.Ol.lng;
            this.topLeftAnchor.y0 =  bs.xl.lat;
        }
        return { 'x1': bs.Ol.lng, 'y1': bs.Ol.lat, 'x2': bs.xl.lng, 'y2': bs.xl.lat };
    }
    private topLeftAnchor = null;
    /**
     * 显示网格
     */
    // TODO 临时
    private showGridPolygons = [];
    showGrid(){
        if(this._map.getZoom()<14){
            return;
         }
        const XY = this.bs();
        var X1 = XY.x1;
        var Y2 = XY.y2;
        var X0 = this.topLeftAnchor.x0;
        var Y0 = this.topLeftAnchor.y0;
        var width = 0.0063;
        var height = 0.0048;
        // var multiple = 1000000;
        if(!!this.topLeftAnchor) {
            X1=X0 + Math.ceil((X1-X0)/width-1)*width;
            Y2=Y0 + Math.ceil((Y2-Y0)/height)*height;
        }
        let polygons = [];
        for (let i = X1; i < XY.x2; i = i + width) {
            for (let j = Y2; j > XY.y1; j = j - height) {
                //此类表示绘制一个多边形覆盖物(注意:一定要下面的Point列表为多变形的顶点,描线顺序为从上到下,从左到右,顺序乱了,绘制出来的多边形也会乱,如图二所示,图二是将顶点的顺序给错了(网格左上端点,网格左下端点,网格右上端点,网格右下端点))
                const polygon = new window.BMap.Polygon([
                    new window.BMap.Point(i, j),    //网格左上端点
                    new window.BMap.Point(i, j-height),  //网格左下端点
                    new window.BMap.Point(i+width, j-height),     //网格右下端点
                    new window.BMap.Point(i+width, j)            //网格右上端点
                ], {strokeColor:"blue", strokeWeight:2, strokeOpacity:0.5, fillOpacity:0.1});
                // polygon .addEventListener("click",function clickFunction(){
                //     alert("你居然敢点我");
                // });
                // _map.addOverlay(polygon);
                polygons.push(polygon);
            }
        }        
        if(!!this.showGridPolygons && this.showGridPolygons.length > 0){
            this.showGridPolygons.forEach(
                polygon => {
                    this._map.removeOverlay(polygon);
                }
            );
        }
        // this._map.addOverlays(polygons);
        polygons.forEach(polygon=>{
            this._map.addOverlay(polygon);
        });
        this.showGridPolygons = polygons;
    }
}