1 | var __extends = (this && this.__extends) || (function () {
|
2 | var extendStatics = function (d, b) {
|
3 | extendStatics = Object.setPrototypeOf ||
|
4 | ({ __proto__: [] } instanceof Array && function (d, b) { d.__proto__ = b; }) ||
|
5 | function (d, b) { for (var p in b) if (b.hasOwnProperty(p)) d[p] = b[p]; };
|
6 | return extendStatics(d, b);
|
7 | }
|
8 | return function (d, b) {
|
9 | extendStatics(d, b);
|
10 | function __() { this.constructor = d; }
|
11 | d.prototype = b === null ? Object.create(b) : (__.prototype = b.prototype, new __());
|
12 | };
|
13 | })();
|
14 | var __assign = (this && this.__assign) || function () {
|
15 | __assign = Object.assign || function(t) {
|
16 | for (var s, i = 1, n = arguments.length; i < n; i++) {
|
17 | s = arguments[i];
|
18 | for (var p in s) if (Object.prototype.hasOwnProperty.call(s, p))
|
19 | t[p] = s[p];
|
20 | }
|
21 | return t;
|
22 | };
|
23 | return __assign.apply(this, arguments);
|
24 | };
|
25 | import * as React from 'react';
|
26 | import { withMap } from './context';
|
27 | var triggerEvents = ['moveend', 'touchend', 'zoomend'];
|
28 | var scales = [
|
29 | 0.01,
|
30 | 0.02,
|
31 | 0.05,
|
32 | 0.1,
|
33 | 0.2,
|
34 | 0.5,
|
35 | 1,
|
36 | 2,
|
37 | 5,
|
38 | 10,
|
39 | 20,
|
40 | 50,
|
41 | 100,
|
42 | 200,
|
43 | 500,
|
44 | 1000,
|
45 | 2 * 1000,
|
46 | 5 * 1000,
|
47 | 10 * 1000
|
48 | ];
|
49 | var positions = {
|
50 | 'top-right': { top: 10, right: 10, bottom: 'auto', left: 'auto' },
|
51 | 'top-left': { top: 10, left: 10, bottom: 'auto', right: 'auto' },
|
52 | 'bottom-right': { bottom: 10, right: 10, top: 'auto', left: 'auto' },
|
53 | 'bottom-left': { bottom: 10, left: 10, top: 'auto', right: 'auto' }
|
54 | };
|
55 | var containerStyle = {
|
56 | position: 'absolute',
|
57 | zIndex: 10,
|
58 | boxShadow: '0px 1px 4px rgba(0, 0, 0, .3)',
|
59 | border: '1px solid rgba(0, 0, 0, 0.1)',
|
60 | right: 50,
|
61 | backgroundColor: '#fff',
|
62 | opacity: 0.85,
|
63 | display: 'flex',
|
64 | flexDirection: 'row',
|
65 | alignItems: 'baseline',
|
66 | padding: '3px 7px'
|
67 | };
|
68 | var scaleStyle = {
|
69 | border: '2px solid #7e8490',
|
70 | boxShadow: '0px 1px 4px rgba(0, 0, 0, .3)',
|
71 | borderTop: 'none',
|
72 | height: 7,
|
73 | borderBottomLeftRadius: 1,
|
74 | borderBottomRightRadius: 1
|
75 | };
|
76 | var POSITIONS = Object.keys(positions);
|
77 | var MEASUREMENTS = ['km', 'mi'];
|
78 | var MILE_IN_KILOMETERS = 1.60934;
|
79 | var MILE_IN_FEET = 5280;
|
80 | var KILOMETER_IN_METERS = 1000;
|
81 | var MIN_WIDTH_SCALE = 60;
|
82 | var ScaleControl = (function (_super) {
|
83 | __extends(ScaleControl, _super);
|
84 | function ScaleControl() {
|
85 | var _this = _super !== null && _super.apply(this, arguments) || this;
|
86 | _this.state = {
|
87 | chosenScale: 0,
|
88 | scaleWidth: MIN_WIDTH_SCALE
|
89 | };
|
90 | _this.setScale = function () {
|
91 | var _a = _this.props, measurement = _a.measurement, map = _a.map;
|
92 | var clientWidth = map._canvas.clientWidth;
|
93 | var _b = map.getBounds(), _ne = _b._ne, _sw = _b._sw;
|
94 | var totalWidth = _this._getDistanceTwoPoints([_sw.lng, _ne.lat], [_ne.lng, _ne.lat], measurement);
|
95 | var relativeWidth = totalWidth / clientWidth * MIN_WIDTH_SCALE;
|
96 | var chosenScale = scales.reduce(function (acc, curr) {
|
97 | if (!acc && curr > relativeWidth) {
|
98 | return curr;
|
99 | }
|
100 | return acc;
|
101 | }, 0);
|
102 | var scaleWidth = chosenScale / totalWidth * clientWidth;
|
103 | _this.setState({
|
104 | chosenScale: chosenScale,
|
105 | scaleWidth: scaleWidth
|
106 | });
|
107 | };
|
108 | return _this;
|
109 | }
|
110 | ScaleControl.prototype.componentDidMount = function () {
|
111 | var _this = this;
|
112 | this.setScale();
|
113 | triggerEvents.forEach(function (event) {
|
114 | _this.props.map.on(event, _this.setScale);
|
115 | });
|
116 | };
|
117 | ScaleControl.prototype.componentWillUnmount = function () {
|
118 | var _this = this;
|
119 | if (this.props.map) {
|
120 | triggerEvents.forEach(function (event) {
|
121 | _this.props.map.off(event, _this.setScale);
|
122 | });
|
123 | }
|
124 | };
|
125 | ScaleControl.prototype._getDistanceTwoPoints = function (x, y, measurement) {
|
126 | if (measurement === void 0) { measurement = 'km'; }
|
127 | var lng1 = x[0], lat1 = x[1];
|
128 | var lng2 = y[0], lat2 = y[1];
|
129 | var R = measurement === 'km' ? 6371 : 6371 / MILE_IN_KILOMETERS;
|
130 | var dLat = this._deg2rad(lat2 - lat1);
|
131 | var dLng = this._deg2rad(lng2 - lng1);
|
132 | var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
|
133 | Math.cos(this._deg2rad(lat1)) *
|
134 | Math.cos(this._deg2rad(lat2)) *
|
135 | Math.sin(dLng / 2) *
|
136 | Math.sin(dLng / 2);
|
137 | var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
|
138 | var d = R * c;
|
139 | return d;
|
140 | };
|
141 | ScaleControl.prototype._deg2rad = function (deg) {
|
142 | return deg * (Math.PI / 180);
|
143 | };
|
144 | ScaleControl.prototype._displayMeasurement = function (measurement, chosenScale) {
|
145 | if (chosenScale >= 1) {
|
146 | return chosenScale + " " + measurement;
|
147 | }
|
148 | if (measurement === 'mi') {
|
149 | return Math.floor(chosenScale * MILE_IN_FEET) + " ft";
|
150 | }
|
151 | return Math.floor(chosenScale * KILOMETER_IN_METERS) + " m";
|
152 | };
|
153 | ScaleControl.prototype.render = function () {
|
154 | var _a = this.props, measurement = _a.measurement, style = _a.style, position = _a.position, className = _a.className, tabIndex = _a.tabIndex;
|
155 | var _b = this.state, chosenScale = _b.chosenScale, scaleWidth = _b.scaleWidth;
|
156 | return (React.createElement("div", { tabIndex: tabIndex, style: __assign({}, containerStyle, positions[position], style), className: className },
|
157 | React.createElement("div", { style: __assign({}, scaleStyle, { width: scaleWidth }) }),
|
158 | React.createElement("div", { style: { paddingLeft: 10 } }, this._displayMeasurement(measurement, chosenScale))));
|
159 | };
|
160 | ScaleControl.defaultProps = {
|
161 | measurement: MEASUREMENTS[0],
|
162 | position: POSITIONS[2]
|
163 | };
|
164 | return ScaleControl;
|
165 | }(React.Component));
|
166 | export { ScaleControl };
|
167 | export default withMap(ScaleControl);
|
168 |
|
\ | No newline at end of file |