google-map-react
Version:
isomorphic google map react component, allows render react components on the google map
162 lines (132 loc) • 5.03 kB
JavaScript
'use strict';
exports.__esModule = true;
var _createClass = function () { function defineProperties(target, props) { for (var i = 0; i < props.length; i++) { var descriptor = props[i]; descriptor.enumerable = descriptor.enumerable || false; descriptor.configurable = true; if ("value" in descriptor) descriptor.writable = true; Object.defineProperty(target, descriptor.key, descriptor); } } return function (Constructor, protoProps, staticProps) { if (protoProps) defineProperties(Constructor.prototype, protoProps); if (staticProps) defineProperties(Constructor, staticProps); return Constructor; }; }(); /* eslint-disable class-methods-use-this */
var _pointGeometry = require('@mapbox/point-geometry');
var _pointGeometry2 = _interopRequireDefault(_pointGeometry);
var _lat_lng = require('./lat_lng');
var _lat_lng2 = _interopRequireDefault(_lat_lng);
var _wrap = require('./wrap');
function _interopRequireDefault(obj) { return obj && obj.__esModule ? obj : { default: obj }; }
function _classCallCheck(instance, Constructor) { if (!(instance instanceof Constructor)) { throw new TypeError("Cannot call a class as a function"); } }
// A single transform, generally used for a single tile to be scaled, rotated, and zoomed.
var Transform = function () {
function Transform(tileSize, minZoom, maxZoom) {
_classCallCheck(this, Transform);
this.tileSize = tileSize || 512; // constant
this._minZoom = minZoom || 0;
this._maxZoom = maxZoom || 52;
this.latRange = [-85.05113, 85.05113];
this.width = 0;
this.height = 0;
this.zoom = 0;
this.center = new _lat_lng2.default(0, 0);
this.angle = 0;
}
Transform.prototype.zoomScale = function zoomScale(zoom) {
return Math.pow(2, zoom);
};
Transform.prototype.scaleZoom = function scaleZoom(scale) {
return Math.log(scale) / Math.LN2;
};
Transform.prototype.project = function project(latlng, worldSize) {
return new _pointGeometry2.default(this.lngX(latlng.lng, worldSize), this.latY(latlng.lat, worldSize));
};
Transform.prototype.unproject = function unproject(point, worldSize) {
return new _lat_lng2.default(this.yLat(point.y, worldSize), this.xLng(point.x, worldSize));
};
// lat/lon <-> absolute pixel coords convertion
Transform.prototype.lngX = function lngX(lon, worldSize) {
return (180 + lon) * (worldSize || this.worldSize) / 360;
};
// latitude to absolute y coord
Transform.prototype.latY = function latY(lat, worldSize) {
var y = 180 / Math.PI * Math.log(Math.tan(Math.PI / 4 + lat * Math.PI / 360));
return (180 - y) * (worldSize || this.worldSize) / 360;
};
Transform.prototype.xLng = function xLng(x, worldSize) {
return x * 360 / (worldSize || this.worldSize) - 180;
};
Transform.prototype.yLat = function yLat(y, worldSize) {
var y2 = 180 - y * 360 / (worldSize || this.worldSize);
return 360 / Math.PI * Math.atan(Math.exp(y2 * Math.PI / 180)) - 90;
};
Transform.prototype.locationPoint = function locationPoint(latlng) {
var p = this.project(latlng);
return this.centerPoint._sub(this.point._sub(p)._rotate(this.angle));
};
Transform.prototype.pointLocation = function pointLocation(p) {
var p2 = this.centerPoint._sub(p)._rotate(-this.angle);
return this.unproject(this.point.sub(p2));
};
_createClass(Transform, [{
key: 'minZoom',
get: function get() {
return this._minZoom;
},
set: function set(zoom) {
this._minZoom = zoom;
this.zoom = Math.max(this.zoom, zoom);
}
}, {
key: 'maxZoom',
get: function get() {
return this._maxZoom;
},
set: function set(zoom) {
this._maxZoom = zoom;
this.zoom = Math.min(this.zoom, zoom);
}
}, {
key: 'worldSize',
get: function get() {
return this.tileSize * this.scale;
}
}, {
key: 'centerPoint',
get: function get() {
return new _pointGeometry2.default(0, 0); // this.size._div(2);
}
}, {
key: 'size',
get: function get() {
return new _pointGeometry2.default(this.width, this.height);
}
}, {
key: 'bearing',
get: function get() {
return -this.angle / Math.PI * 180;
},
set: function set(bearing) {
this.angle = -(0, _wrap.wrap)(bearing, -180, 180) * Math.PI / 180;
}
}, {
key: 'zoom',
get: function get() {
return this._zoom;
},
set: function set(zoom) {
var zoomV = Math.min(Math.max(zoom, this.minZoom), this.maxZoom);
this._zoom = zoomV;
this.scale = this.zoomScale(zoomV);
this.tileZoom = Math.floor(zoomV);
this.zoomFraction = zoomV - this.tileZoom;
}
}, {
key: 'x',
get: function get() {
return this.lngX(this.center.lng);
}
}, {
key: 'y',
get: function get() {
return this.latY(this.center.lat);
}
}, {
key: 'point',
get: function get() {
return new _pointGeometry2.default(this.x, this.y);
}
}]);
return Transform;
}();
exports.default = Transform;