zhanghua
2023-01-29 a3a502b6340a610ca40fbc4ecb055eb9d1412976
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
/**
 * Projection class for Baidu Spherical Mercator
 *
 * @class BaiduSphericalMercator
 */
L.Projection.BaiduSphericalMercator = {
    /**
     * Project latLng to point coordinate
     *
     * @method project
     * @param {Object} latLng coordinate for a point on earth
     * @return {Object} leafletPoint point coordinate of L.Point
     */
    project: function(latLng) {
        var projection = new BMap.MercatorProjection();
        var originalPoint = new BMap.Point(latLng.lng, latLng.lat);
        //var baiduPoint = window.translatePoint(originalPoint);
        var point = projection.lngLatToPoint(originalPoint);
        var leafletPoint = new L.Point(point.x, point.y);
        return leafletPoint;
    },
 
    /**
     * unproject point coordinate to latLng
     *
     * @method unproject
     * @param {Object} bpoint baidu point coordinate
     * @return {Object} latitude and longitude
     */
    unproject: function (bpoint) {
        var projection= new BMap.MercatorProjection();
        var point = projection.pointToLngLat(
            new BMap.Pixel(bpoint.x, bpoint.y)
        );
        var latLng = new L.LatLng(point.lat, point.lng);
        return latLng;
    },
 
    /**
     * Don't know how it used currently.
     *
     * However, I guess this is the range of coordinate.
     * Range of pixel coordinate is gotten from
     * BMap.MercatorProjection.lngLatToPoint(180, -90) and (180, 90)
     * After getting max min value of pixel coordinate, use
     * pointToLngLat() get the max lat and Lng.
     */
    bounds: (function () {
        var MAX_X= 20037726.37;
        var MIN_Y= -11708041.66;
        var MAX_Y= 12474104.17;
        var bounds = L.bounds(
            [-MAX_X, MIN_Y], //-180, -71.988531
            [MAX_X, MAX_Y]  //180, 74.000022
        );
        var MAX = 33554432;
        bounds = new L.Bounds(
            [-MAX, -MAX],
            [MAX, MAX]
        );
        return bounds;
    })()
};
 
/**
 * Coordinate system for Baidu EPSGB3857
 *
 * @class EPSGB3857
 */
L.CRS.EPSGB3857 = L.extend({}, L.CRS, {
    code: 'EPSG:B3857',
    projection: L.Projection.BaiduSphericalMercator,
 
    transformation: (function () {
        var z = -18 - 8;
        var scale = Math.pow(2, z);
        return new L.Transformation(scale, 0.5, -scale, 0.5);
    }())
});
 
/**
 * Tile layer for Baidu Map
 *
 * @class BaiduLayer
 */
L.TileLayer.BaiduLayer = L.TileLayer.extend({
    statics: {
        attribution: '© 2014 Baidu - GS(2012)6003;- Data © <a target="_blank" href="http://www.navinfo.com/">NavInfo</a> & <a target="_blank" href="http://www.cennavi.com.cn/">CenNavi</a> & <a target="_blank" href="http://www.365ditu.com/">DaoDaoTong</a>'
    },
 
    options: {
        minZoom: 3,
        maxZoom: 19
    },
 
    initialize: function (type, options) {
        var desc = L.TileLayer.BaiduLayer.desc;
        type = type || 'Normal.Map';
        var parts = type.split('.');
        var mapName = parts[0],
            mapType = parts[1],
            mapStyle = parts[2];
        var url = desc[mapName][mapType];
        if(mapName === 'CustomStyle') {
          if(mapStyle) {
            url = url+'&customid='+mapStyle;
          }else if(options.styles) {
            url = url+'&styles='+options.styles;
          }
        }
        options = options || {};
        options.subdomains = desc[mapName].subdomains || desc.subdomains;
        options.attribution = L.TileLayer.BaiduLayer.attribution;
        L.TileLayer.prototype.initialize.call(this, url, options);
    },
 
    getTileUrl: function (coords) {
        var offset = Math.pow(2, coords.z - 1),
            x = coords.x - offset,
            y = offset - coords.y - 1,
            baiduCoords = L.point(x, y);
        baiduCoords.z = coords.z;
        return L.TileLayer.prototype.getTileUrl.call(this, baiduCoords);
    }
});
 
L.TileLayer.BaiduLayer.desc = {
    Normal: {
        Map: 'http://online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=pl'
    },
    Satellite: {
        Map: 'http://shangetu{s}.map.bdimg.com/it/u=x={x};y={y};z={z};v=009;type=sate&fm=46',
        Road: 'http://online{s}.map.bdimg.com/tile/?qt=tile&x={x}&y={y}&z={z}&styles=sl'
    },
    CustomStyle: {
      Map: 'http://api{s}.map.bdimg.com/customimage/tile?&x={x}&y={y}&z={z}',
      subdomains: '012'
    },
    subdomains: '0123456789'
};
 
L.tileLayer.baiduLayer = function (type, options) {
    return new L.TileLayer.BaiduLayer(type, options);
};