UNPKG

@mint-ui/map

Version:

- React map library - Control various map with one interface - Google, Naver, Kakao map supported now - Typescript supported - Canvas marker supported

160 lines (118 loc) 5.75 kB
'use strict'; Object.defineProperty(exports, '__esModule', { value: true }); var PositionMirror = /** @class */ function () { function PositionMirror(lat, lng) { this.lat = lat; this.lng = lng; } return PositionMirror; }(); var GeoCalulator = /** @class */ function () { function GeoCalulator() {} GeoCalulator.computeDistanceKiloMeter = function (pos1, pos2) { var dLat = this.deg2rad(pos2.lat - pos1.lat); var dLon = this.deg2rad(pos2.lng - pos1.lng); var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(this.deg2rad(pos1.lat)) * Math.cos(this.deg2rad(pos2.lat)) * Math.sin(dLon / 2) * Math.sin(dLon / 2); var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a)); var d = this.EARTH_EQUATORIAL_RADIUS_KM * c; // Distance in km return d; }; GeoCalulator.deg2rad = function (deg) { return deg * (Math.PI / 180); }; GeoCalulator.convertMeterToLatitudeValue = function (meter) { return meter * this.LATITUDE_POSITION_VALUE_PER_METER; }; GeoCalulator.convertLatitudeToMeterValue = function (lat) { return lat * this.METER_VALUE_PER_LATITUDE; }; GeoCalulator.convertLongitudeToMeterValue = function (lat, lng) { return lng * this.calculateLongitudeValueWithLatitudeInMeter(lat); }; GeoCalulator.getCacheUnitOfLongitudeValueWithLatitudeInMeter = function (lat) { return lat.toFixed(2); }; GeoCalulator.getCacheOfLongitudeValueWithLatitudeInMeter = function (latUnit) { return this.CACHE_OF_LNG_PER_METER.get(latUnit); }; GeoCalulator.setCacheOfLongitudeValueWithLatitudeInMeter = function (latUnit, lngValue) { if (this.CACHE_OF_LNG_PER_METER.size > 10) { this.CACHE_OF_LNG_PER_METER.clear(); } this.CACHE_OF_LNG_PER_METER.set(latUnit, lngValue); }; GeoCalulator.calculateLongitudeValueWithLatitudeInMeter = function (lat) { // const t = Date.now() // Cache check var latUnit = this.getCacheUnitOfLongitudeValueWithLatitudeInMeter(lat); var fromCache = this.getCacheOfLongitudeValueWithLatitudeInMeter(latUnit); if (fromCache !== undefined) { // console.log(`cache hit!! ${Date.now() - t} ms`, fromCache, latUnit, this.CACHE_OF_LNG_PER_METER.size); return fromCache; } // Convert latitude and longitude to radians var latRad = lat * Math.PI / 180; // Calculate Earth's radius at the given latitude var radius = this.EARTH_EQUATORIAL_RADIUS * Math.sqrt(1 - Math.pow(this.EARTH_ECCENTRICITY * Math.sin(latRad), 2)); // Calculate the length of one degree of longitude in meters var distance = 2 * Math.PI * radius * Math.cos(latRad) / 360; // Cache set this.setCacheOfLongitudeValueWithLatitudeInMeter(latUnit, distance); // console.log(`calculated ${Date.now() - t} ms`) return distance; }; GeoCalulator.computeNextPositionAndDistances = function (context) { var pos1 = context.pos1, pos2 = context.pos2, prevPos2 = context.prevPos2, velocityKmh = context.velocityKmh, prevVelocityKmh = context.prevVelocityKmh, elapsedTimeMs = context.elapsedTimeMs; // console.log('velocityKmh / elapsedTimeMs',velocityKmh , elapsedTimeMs); //총 가야할 거리 (km) if (pos2 !== prevPos2) { //목표가 바뀌면 거리 및 비율 재계산 context.totalDistance = this.computeDistanceKiloMeter(pos1, pos2); context.currDistance = 0; context.prevPos2 = pos2; } var totalDistance = context.totalDistance; // console.log('totalDistance', totalDistance); //ms 속으로 환산 if (velocityKmh !== prevVelocityKmh) { //속도가 바뀌면 재계산 context.vPerMs = velocityKmh / this.MS_FROM_HOUR; context.prevVelocityKmh = velocityKmh; } var vPerMs = context.vPerMs; //console.log('vPerMs', vPerMs); //실제 가는 거리 계산 var nextDistance = context.distanceRemain ? context.distanceRemain : context.currDistance + elapsedTimeMs * vPerMs; //console.log('nextDistance', nextDistance); //목표점까지 이동 후에도 남는 거리 context.currDistance = nextDistance; if (totalDistance < context.currDistance) { //이동 거리가 현재 목표점을 넘어가는 경우 context.distanceRemain = context.currDistance - totalDistance; context.nextPos = pos2; return context; } else { context.distanceRemain = 0; } //각 축으로 나가야할 비율 var ratio = nextDistance / totalDistance; //console.log('ratio', ratio); //방향값 체크 var latCalib = pos2.lat > pos1.lat ? 1 : -1; var lngCalib = pos2.lng > pos1.lng ? 1 : -1; //각 축에 보정된 새로운 지점 리턴 var newPos = new PositionMirror(pos1.lat + (pos2.lat - pos1.lat) * ratio, pos1.lng + (pos2.lng - pos1.lng) * ratio); if ((latCalib === 1 && pos2.lat <= newPos.lat || latCalib === -1 && pos2.lat >= newPos.lat) && (lngCalib === 1 && pos2.lng <= newPos.lng || lngCalib === -1 && pos2.lng >= newPos.lng)) { newPos = pos2; } // console.log('newPos', newPos); //console.log('==============================================================\n'); context.nextPos = newPos; return context; }; GeoCalulator.EARTH_EQUATORIAL_RADIUS = 6378137; //meter (6,378,137 m) GeoCalulator.EARTH_EQUATORIAL_RADIUS_KM = GeoCalulator.EARTH_EQUATORIAL_RADIUS / 1000; GeoCalulator.EARTH_ECCENTRICITY = 0.08181919; GeoCalulator.METER_VALUE_PER_LATITUDE = 110.32 * 1000; //위도 기준 1도는 110.32km GeoCalulator.LATITUDE_POSITION_VALUE_PER_METER = 1 / GeoCalulator.METER_VALUE_PER_LATITUDE; GeoCalulator.CACHE_OF_LNG_PER_METER = new Map(); GeoCalulator.MS_FROM_HOUR = 60 * 60 * 1000; return GeoCalulator; }(); exports.GeoCalulator = GeoCalulator;