UNPKG

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