@zenlocator/geo
Version:
2 lines • 9.31 kB
JavaScript
!function(t,s){"object"==typeof exports&&"undefined"!=typeof module?module.exports=s():"function"==typeof define&&define.amd?define(s):t.Geo=s()}(this,function(){"use strict";var t=function(t,s,n){var i=0;switch(t=parseInt(t,10),s=s.toUpperCase(),n=n.toUpperCase(),s){case this.distanceUnits.KILOMETERS:case this.distanceUnits.KM:i=1e3*t;break;case this.distanceUnits.METERS:i=t;break;case this.distanceUnits.LEAGUES:i=5556*t;break;case this.distanceUnits.MILES:i=1609.344*t;break;case this.distanceUnits.YARDS:i=t/1.0936;break;case this.distanceUnits.FEET:i=.3048*t;break;case this.distanceUnits.INCHES:i=.0254*t;break;case this.distanceUnits.NAUTICAL_MILES:i=1852*t;break;case this.distanceUnits.SHEPPEYS:i=t/.00071428571}return this.formatDistance(i,n)},s=function(t){if("number"==typeof t)return this.coordFormats.D;if("string"==typeof t){var s=t.match(/[+-]?\d+(\.\d+)?/g);if(s){if(3===s.length)return this.coordFormats.DMS;if(2===s.length)return this.coordFormats.DM;if(1===s.length)return-1===t.indexOf("°")?this.coordFormats.D:this.coordFormats.DD}}},n=function(t){var s={sw:0,nw:0,ne:0,se:0},n={lat:-90,lng:1},i=[];if(this.isArray(t)&&t.length>=2)i=this.orderByDistance(n,t);else{if(!(this.isPlainObject(t)&&Object.keys(t).length>=2))return;i=this.orderByDistance(n,this.values(t))}if(i.length){s.sw=this.formatCoords({lat:i[0].lat,lng:i[0].lng});var r=i.length-1;if(s.ne=this.formatCoords({lat:i[r].lat,lng:i[r].lng}),this.parseCoord(s.ne.lat)<this.parseCoord(s.sw.lat)||this.parseCoord(s.ne.lng)<this.parseCoord(s.sw.lng)){var e=[s.ne,s.sw];s.sw=e[0],s.ne=e[1]}return s.nw=this.formatCoords({lat:s.ne.lat,lng:s.sw.lng}),s.se=this.formatCoords({lat:s.sw.lat,lng:s.ne.lng}),s}},i=function(t){var s=!(arguments.length>1&&void 0!==arguments[1])||arguments[1];if(void 0===t)return t;var n=[],i=void 0,r=void 0,e=void 0;if(t=this.parseCoord(t,!1),this.settings.coordsFormat===this.coordFormats.D)return parseFloat(t.toFixed(this.settings.coordsPrecision));if(this.settings.coordsFormat===this.coordFormats.DM?i=1:this.settings.coordsFormat===this.coordFormats.DMS&&(i=2),i)for(var o=0;o!==i;o++)n.push(parseInt(t,10)),r=t%1,t=60*r;return n.push(parseFloat(t.toFixed(this.settings.coordsPrecision))),e=s?n[0]>0?"N":"S":n[0]>0?"E":"W",n=n.map(function(t){return Math.abs(t)}),n=3===n.length?n[0]+"° "+n[1]+"' "+n[2]+'" '+e:2===n.length?n[0]+"° "+n[1]+"' "+e:1===n.length?n[0]+"° "+e:void 0},r=function(t){return t={lat:this.parseCoord(t.lat),lng:this.parseCoord(t.lng)},(0===t.lat&&-180===t.lng||0===t.lat&&180===t.lng)&&(t.lng=0),{lat:this.formatCoord(t.lat,!0),lng:this.formatCoord(t.lng,!1)}},e=function(t){var s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:this.settings.distanceUnits,n=parseFloat(t),i=void 0;switch(s){case this.distanceUnits.KILOMETERS:case this.distanceUnits.KM:n*=.001,i=n<1?2:1;break;case this.distanceUnits.LEAGUES:n*=179986e-9,i=1;break;case this.distanceUnits.MILES:n*=621371e-9,i=n<1?2:1;break;case this.distanceUnits.YARDS:n*=1.09361,i=0;break;case this.distanceUnits.FEET:n*=3.28084,i=0;break;case this.distanceUnits.INCHES:n*=39.3701,i=0;break;case this.distanceUnits.NAUTICAL_MILES:n*=539957e-9,i=n<1?2:1;break;case this.distanceUnits.SHEPPEYS:n*=.00071428571,i=2}return this.settings.distancePrecision!==this.FLEXIBLE_DISTANCE_PRECISION&&(i=this.settings.distancePrecision),parseFloat(n.toFixed(i))},o=function(t,s){t={lat:this.parseCoord(t.lat,!0),lng:this.parseCoord(t.lng,!1)};var n=this.getDestinationPoint(t,s,225),i=this.getDestinationPoint(t,s,45);return this.formatBounds({sw:n,ne:i})},a=function(){var t=arguments.length>0&&void 0!==arguments[0]?arguments[0]:[];if(t.length){if(1===t.length)return this.formatCoords({lat:t[0].lat,lng:t[0].lng});for(var s=0,n=0,i=0,r=void 0,e=void 0,o=0;o!==t.length;o++){var a=t[o];r=this.toRad(this.parseCoord(a.lat)),e=this.toRad(this.parseCoord(a.lng)),s+=Math.cos(r)*Math.cos(e),n+=Math.cos(r)*Math.sin(e),i+=Math.sin(r)}var h=t.length;s/=h,n/=h,i/=h,e=Math.atan2(n,s);var c=Math.sqrt(s*s+n*n);return r=Math.atan2(i,c),this.formatCoords({lat:180*r/Math.PI,lng:180*e/Math.PI})}},h=function(t){var s=arguments.length>1&&void 0!==arguments[1]?arguments[1]:[],n=void 0,i=this.orderByDistance(t,s);return i.length&&(n=i[0]),n},c=function(t,s,n){var i=arguments.length>3&&void 0!==arguments[3]?arguments[3]:this.RADIUS_EQUILATERAL,r=this.parseCoord(t.lat,!0),e=this.parseCoord(t.lng,!1),o=Number(s)/i,a=this.toRad(Number(n)),h=this.toRad(Number(r)),c=this.toRad(Number(e)),d=Math.asin(Math.sin(h)*Math.cos(o)+Math.cos(h)*Math.sin(o)*Math.cos(a)),l=c+Math.atan2(Math.sin(a)*Math.sin(o)*Math.cos(h),Math.cos(o)-Math.sin(h)*Math.sin(d));return l=(l+3*Math.PI)%(2*Math.PI)-Math.PI,this.formatCoords({lat:this.toDeg(d),lng:this.toDeg(l)})},d=function(t,s){var n=function(t){return t*(Math.PI/180)},i=this.parseCoord(t.lat,!0),r=this.parseCoord(t.lng,!1),e=this.parseCoord(s.lat,!0),o=this.parseCoord(s.lng,!1),a=n(e-i),h=n(o-r),c=Math.sin(a/2)*Math.sin(a/2)+Math.cos(n(i))*Math.cos(n(e))*Math.sin(h/2)*Math.sin(h/2);return this.formatDistance(12742e3*Math.atan2(Math.sqrt(c),Math.sqrt(1-c)))},l=function(t,s){return t={lat:this.parseCoord(t.lat),lng:this.parseCoord(t.lng)},s=this.parseBounds(s),t.lat>=s.sw.lat&&t.lat<=s.ne.lat&&t.lng>=s.nw.lng&&t.lng<=s.se.lng},f=function(t,s,n){var i=this.settings.distanceUnits;this.settings.distanceUnits=this.distanceUnits.METER;var r=this.getDistance(t,s)<=n;return this.settings.distanceUnits=i,r},u=function(t){return(t=this.parseCoord(t))>=-90&&t<=90},g=function(t){return(t=this.parseCoord(t))>=-180&&t<=180},p=function(t,s){var n=this;if(this.isPlainObject(s)||this.isArray(s)){var i=this.isArray(s)?[]:{},r=[],e=[],o=function(s,i){var o={};i&&(o.key=i),o.value=s,o.distance=n.getDistance(t,s),r.push(o),e.push(o.distance)};if(this.isArray(s)){var a=!0,h=!1,c=void 0;try{for(var d,l=s[Symbol.iterator]();!(a=(d=l.next()).done);a=!0){o(d.value)}}catch(t){h=!0,c=t}finally{try{!a&&l.return&&l.return()}finally{if(h)throw c}}}else for(var f in s)o(s[f],f);e.sort(function(t,s){return t-s});for(var u in e)!function(t){var s=r.filter(function(s){if(s.distance===e[t])return s});for(var o in s){var a=s[o],h=n.formatCoords(a.value);a.hasOwnProperty("key")?i[a.key]=h:i.push(h)}}(u);return i}},E="function"==typeof Symbol&&"symbol"==typeof Symbol.iterator?function(t){return typeof t}:function(t){return t&&"function"==typeof Symbol&&t.constructor===Symbol&&t!==Symbol.prototype?"symbol":typeof t},M=function(t,s){if(!(t instanceof s))throw new TypeError("Cannot call a class as a function")},v=Object.assign||function(t){for(var s=1;s<arguments.length;s++){var n=arguments[s];for(var i in n)Object.prototype.hasOwnProperty.call(n,i)&&(t[i]=n[i])}return t},C=function(t){return t=v({},this.formatBounds(t)),t&&(t={sw:{lat:this.parseCoord(t.sw.lat),lng:this.parseCoord(t.sw.lng)},nw:{lat:this.parseCoord(t.nw.lat),lng:this.parseCoord(t.nw.lng)},ne:{lat:this.parseCoord(t.ne.lat),lng:this.parseCoord(t.ne.lng)},se:{lat:this.parseCoord(t.se.lat),lng:this.parseCoord(t.se.lng)}}),t},m=function(t){var s=!(arguments.length>1&&void 0!==arguments[1])||arguments[1],n=this.detectCoordFormat(t),i=[],r=void 0;if(n)if(n===this.coordFormats.D)r=parseFloat(t);else{if("string"==typeof t&&(t=t.toLowerCase(),i=t.match(/[+-]?\d+(\.\d+)?/g).map(function(t){return parseFloat(t)})),(1===i.length&&n!==this.coordFormats.DD||2===i.length&&n!==this.coordFormats.DM||3===i.length&&n!==this.coordFormats.DMS||i.length<1||i.length>3)&&(i=[]),i.length){var e=0,o=!0,a=!1,h=void 0;try{for(var c,d=i.reverse()[Symbol.iterator]();!(o=(c=d.next()).done);o=!0)r=c.value,r+=e,e=r/60}catch(t){a=!0,h=t}finally{try{!o&&d.return&&d.return()}finally{if(a)throw h}}}!r||"string"!=typeof t||-1===t.indexOf("s")&&-1===t.indexOf("w")||(r*=-1)}return r&&s&&(r=parseFloat(r.toFixed(this.settings.coordsPrecision))),r},S=function t(){var s=arguments.length>0&&void 0!==arguments[0]?arguments[0]:{};M(this,t);for(var n in s)s.hasOwnProperty(n)&&("string"==typeof s[n]&&(s[n]=s[n].toUpperCase()),-1!==["distancePrecision","coordsPrecision"].indexOf(n)&&(s[n]=parseInt(s[n],10)));this.distanceUnits={KILOMETERS:"KILOMETERS",KM:"KM",METERS:"METERS",LEAGUES:"LEAGUES",MILES:"MILES",YARDS:"YARDS",FEET:"FEET",INCHES:"INCHES",NAUTICAL_MILES:"NAUTICAL_MILES",SHEPPEYS:"SHEPPEYS"},this.FLEXIBLE_DISTANCE_PRECISION=-1,this.coordFormats={DMS:"DMS",DM:"DM",DD:"DD",D:"D"},this.toRad=function(t){return t*Math.PI/180},this.toDeg=function(t){return 180*t/Math.PI},this.isPlainObject=function(t){return"object"===(void 0===t?"undefined":E(t))&&t.constructor===Object},this.isArray=function(t){return"[object Array]"===Object.prototype.toString.call(t)},this.values=function(t){return Object.keys(t).map(function(s){return t[s]})},this.RADIUS_MEAN=6371e3,this.RADIUS_EQUILATERAL=6378100,this.RADIUS_POLAR=6356800,this.settings=v({distanceUnits:this.distanceUnits.METER,distancePrecision:this.FLEXIBLE_DISTANCE_PRECISION,coordsFormat:this.coordFormats.D,coordsPrecision:6},s)};return S.prototype=v({},S.prototype,{convertDistance:t,detectCoordFormat:s,formatBounds:n,formatCoord:i,formatCoords:r,formatDistance:e,getBounds:o,getCenter:a,getClosest:h,getDestinationPoint:c,getDistance:d,isPointInBounds:l,isPointInCircle:f,isValidLat:u,isValidLng:g,orderByDistance:p,parseBounds:C,parseCoord:m}),S});
//# sourceMappingURL=dist/geo.min.map