|
| 1 | +{ |
| 2 | +let BMAP_URL = "https://api.map.baidu.com/api?v=2.0&ak=gfXoaMam8YmLlPZ05kKUw7C7CLykEn3T&s=1"; |
| 3 | +let OPRHelperBaiduMap = class extends OPRHelperMap { |
| 4 | + constructor(mapid, settings) { |
| 5 | + super(mapid); |
| 6 | + this.road = false; |
| 7 | + this.satellite = false; |
| 8 | + this.pcontainer = settings.pcontainer; |
| 9 | + this.map = new BMap.Map(settings.mcontainer); |
| 10 | + this.map.centerAndZoom(new BMap.Point(116, 39), 15); // fuck baidu! map won't work if centerAndZoom isn't called |
| 11 | + this.map.enableScrollWheelZoom(); |
| 12 | + this.map.enableContinuousZoom(); |
| 13 | + this.map.addControl(new BMap.NavigationControl({ |
| 14 | + anchor: BMAP_ANCHOR_BOTTOM_RIGHT, |
| 15 | + type: BMAP_NAVIGATION_CONTROL_ZOOM, |
| 16 | + })); |
| 17 | + this.map.addControl(new BMap.ScaleControl({ |
| 18 | + anchor: BMAP_ANCHOR_BOTTOM_LEFT, |
| 19 | + })); |
| 20 | + this.ready() |
| 21 | + } |
| 22 | + |
| 23 | + setZoom(z) { |
| 24 | + this.map.setZoom(z); |
| 25 | + } |
| 26 | + |
| 27 | + setCenter(lng, lat) { |
| 28 | + [lng, lat] = OPRHelperBaiduMap.mars2baidu(lng, lat); |
| 29 | + this.map.setCenter(new BMap.Point(lng, lat)); |
| 30 | + } |
| 31 | + |
| 32 | + addMarker(lng, lat, icon, offset) { |
| 33 | + [lng, lat] = OPRHelperBaiduMap.mars2baidu(lng, lat); |
| 34 | + var pos = new BMap.Point(lng, lat); |
| 35 | + var opt = {} |
| 36 | + if (icon) { |
| 37 | + opt.icon = new BMap.Icon(icon); |
| 38 | + } |
| 39 | + var marker = new BMap.Marker(pos, opt); |
| 40 | + this.map.addOverlay(marker); |
| 41 | + } |
| 42 | + |
| 43 | + showSatellite(flag) { |
| 44 | + var mapType; |
| 45 | + if (flag === undefined || flag) { |
| 46 | + this.satellite = true; |
| 47 | + if (this.road) |
| 48 | + mapType = BMAP_HYBRID_MAP; |
| 49 | + else |
| 50 | + mapType = BMAP_SATELLITE_MAP; |
| 51 | + } else { |
| 52 | + this.satellite = false; |
| 53 | + mapType = BMAP_NORMAL_MAP; |
| 54 | + } |
| 55 | + this.map.setMapType(mapType); |
| 56 | + } |
| 57 | + |
| 58 | + showRoad(flag) { |
| 59 | + var mapType = BMAP_NORMAL_MAP; |
| 60 | + if (flag === undefined || flag) { |
| 61 | + this.road = true; |
| 62 | + if (this.satellite) |
| 63 | + mapType = BMAP_HYBRID_MAP; |
| 64 | + } else { |
| 65 | + this.road= false; |
| 66 | + if (this.satellite) |
| 67 | + mapType = BMAP_SATELLITE_MAP; |
| 68 | + } |
| 69 | + this.map.setMapType(mapType); |
| 70 | + } |
| 71 | + |
| 72 | + showPanorama(lng, lat) { |
| 73 | + [lng, lat] = OPRHelperBaiduMap.mars2baidu(lng, lat); |
| 74 | + OPRHelperBaiduMap.getPanoramaInfo(lng, lat) |
| 75 | + .then((data) => { |
| 76 | + if (!this.panorama) { |
| 77 | + this.panorama = |
| 78 | + new BMap.Panorama(this.pcontainer, { |
| 79 | + navigationControl: true, |
| 80 | + linksControl: true, |
| 81 | + }); |
| 82 | + } |
| 83 | + var pos = new BMap.Point(lng, lat); |
| 84 | + this.panorama.setPosition(pos); |
| 85 | + this.panorama.addOverlay( |
| 86 | + new BMap.PanoramaLabel( |
| 87 | + 'Portal Here', |
| 88 | + {position: pos} |
| 89 | + ) |
| 90 | + ); |
| 91 | + this.panorama.show(); |
| 92 | + }); |
| 93 | + } |
| 94 | + |
| 95 | + static getPanoramaInfo(lng, lat) { |
| 96 | + return new Promise((resolve, reject) => { |
| 97 | + var service = new BMap.PanoramaService(); |
| 98 | + service.getPanoramaByLocation(new BMap.Point(lng, lat), |
| 99 | + (data) => { |
| 100 | + if (data == null) |
| 101 | + reject(); |
| 102 | + else |
| 103 | + resolve(data); |
| 104 | + }); |
| 105 | + }); |
| 106 | + } |
| 107 | + |
| 108 | + // stolen from http://blog.csdn.net/coolypf/article/details/8569813 |
| 109 | + static mars2baidu(lng, lat) { |
| 110 | + const xpi = 3.14159265358979324 * 3000.0 / 180.0; |
| 111 | + var z = Math.sqrt(lng * lng + lat * lat) + 2e-5 * Math.sin(lat * xpi); |
| 112 | + var theta = Math.atan2(lat, lng) + 3e-6 * Math.cos(lng * xpi); |
| 113 | + return [ |
| 114 | + z * Math.cos(theta) + 0.0065, // lng |
| 115 | + z * Math.sin(theta) + 0.006 // lat |
| 116 | + ]; |
| 117 | + } |
| 118 | +} |
| 119 | + |
| 120 | +OPRHelperMapFactory.register('baidu', OPRHelperBaiduMap, () => { |
| 121 | + return new Promise((resolve, reject) => { |
| 122 | + BMAP_URL += '&callback=window.onbmapload_'; |
| 123 | + window.onbmapload_ = () => { |
| 124 | + delete window.onbmapload_; |
| 125 | + setTimeout(resolve, 2000); |
| 126 | + } |
| 127 | + loadScript(BMAP_URL); |
| 128 | + }); |
| 129 | +}); |
| 130 | +} |
0 commit comments