UNPKG

wilddog-location

Version:

基于Wilddog的地理位置范围查询

8 lines 23.2 kB
/*! * js库 wilddog.Location 可以基于地理坐标位置存储和查询一组key值,它的核心是,只存储位置坐标的key值。 * 这最大的好处是能够实时地在给定的地理区域内查询key的集合,实现实时区域查询,实时轨迹查询。 * * Location 0.0.0 * License: MIT */ var RealtimeLocation=function(){"use strict";function e(e,t){return E(e),T(t),{geohash:t,location:[e.latitude(),e.longitude()],timestamp:e.timestamp(),attributes:e.customAttributes()}}function t(e){if(null!==e&&e.hasOwnProperty("timestamp")&&e.hasOwnProperty("location")&&Array.isArray(e.location)&&2===e.location.length){var t=new f({location:e.location,timestamp:e.timestamp,customAttributes:e.attributes});return t}throw new Error("Unexpected WildLocation location object encountered: "+JSON.stringify(e))}var n="0.2.4",r=function(e,t,n){return new Promise(function(n,r){var i="location-api?";for(var o in t)t.hasOwnProperty(o)&&(i+=o+"="+t[o]+"&");i=i.substring(0,i.length-1);{var a=require("https");a.get(e+i,function(e){e.on("data",function(e){n(e)})}).on("error",function(e){r(e)})}})},i={type:"node",getPlatformInfo:function(){var e={};return e.deviceid="node",e.cv=n,e.platform="node",e.ua="node",e}},o=function(e){var t,n=e&&e.sampleType||"timeInterval",r=e&&e.timeInterval||1e3,i=e&&e.distanceInterval||0,o=new AMap.Map("container",{resizeEnable:!0});o.plugin("AMap.Geolocation",function(){t=new AMap.Geolocation({enableHighAccuracy:!0,timeout:1e4})}),this.sampleType=function(){return n},this.timeInterval=function(){return r},this.distanceInterval=function(){return i};var a,l,s=[];this._on=function(e){s.push(e),a||(a=setInterval(function(){t&&t.getCurrentPosition(function(e,t){var r=null,o=null;if("error"==e)r=t;else if("complete"==e){var a={};a.location=[t.position.getLat(),t.position.getLng()],o=new f(a)}var u=function(){for(var e=0;e<s.length;e++)s[e](o,r)};if("timeInterval"==n)u();else if(o&&l){var c=d.distance(o,l);c>=i&&(l=o,u())}else l=o,u()})},r))},this._off=function(e){var t=s.indexOf(e);-1!=t&&s.splice(t,1),0===s.length&&(clearInterval(a),a=null)},this.getCurrentPosition=function(){return new Promise(function(e,n){t.getCurrentPosition(function(t,r){if("complete"==t){var i={};i.location=[r.position.getLat(),r.position.getLng()];var o=new f(i);e(o)}else"error"==t&&n(r)})})}},a=function(e){var t=e&&e.sampleType||"timeInterval",n=e&&e.timeInterval||1e3,r=e&&e.distanceInterval||0,i=e.AMapWX;this.sampleType=function(){return t},this.timeInterval=function(){return n},this.distanceInterval=function(){return r};var o,a,l=[];this._on=function(e){l.push(e),o||(o=setInterval(function(){i&&i.getRegeo({success:function(e){var n=null,i=null,o={};o.location=[e[0].latitude,e[0].longitude],i=new f(o);var s=function(){for(var e=0;e<l.length;e++)l[e](i,n)};if("timeInterval"==t)s();else if(i&&a){var u=d.distance(i,a);u>=r&&(a=i,s())}else a=i,s()},fail:function(e){for(var t=e,n=null,r=0;r<l.length;r++)l[r](n,t)}})},n))},this._off=function(e){var t=l.indexOf(e);-1!=t&&l.splice(t,1),0===l.length&&(clearInterval(o),o=null)},this.getCurrentPosition=function(){return new Promise(function(e,t){i.getRegeo({success:function(t){var n=null,r={};r.location=[t[0].latitude,t[0].longitude],n=new f(r),e(n)},fail:function(e){t(e)}})})}},l=function(e){if(this.cancel=function(){"undefined"!=typeof t&&(t(),t=void 0)},"function"!=typeof e)throw new Error("callback must be a function");var t=e},s=function(e,n){function r(e,t,n,r){k[e].forEach(function(e){"undefined"==typeof n||null===n?e(t,null,null):e(t,n,r)})}function i(){k.ready.forEach(function(e){e()})}function o(e){var t=e.split(":");if(2!==t.length)throw new Error("Invalid internal state! Not a valid geohash query: "+e);return t}function a(e){if(2!==e.length)throw new Error("Not a valid geohash query: "+e);return e[0]+":"+e[1]}function s(e,t){var n=I.orderByChild("geohash").startAt(e[0]).endAt(e[1]);n.off("child_added",t.childAddedCallback),n.off("child_removed",t.childRemovedCallback),n.off("child_changed",t.childChangedCallback),n.off("value",t.valueCallback)}function u(){for(var e=Object.keys(x),t=e.length,n=0;t>n;++n){var r=e[n],i=x[r];if(i.active===!1){{o(r)}delete x[r]}}for(e=Object.keys(T),t=e.length,n=0;t>n;++n){var a=e[n];if(!f(T[a].geohash)){if(T[a].isInQuery)throw new Error("Internal State error, trying to remove location that is still in query");delete T[a]}}_=!1,null!==C&&(clearTimeout(C),C=null)}function c(e,t){E(t);var n,i,o=T.hasOwnProperty(e)?T[e].isInQuery:!1,a=T.hasOwnProperty(e)?T[e].location:null;n=d.distance(t,j),i=N>=n,T[e]={location:t,distanceFromCenter:n,isInQuery:i,geohash:O(t,h)},i&&!o?r("key_entered",e,t,n):!i||null===a||t.latitude()===a.latitude()&&t.longitude()===a.longitude()?!i&&o&&r("key_exited",e,t,n):r("key_moved",e,t,n)}function f(e){for(var t=Object.keys(x),n=t.length,r=0;n>r;++r){var i=t[r];if(x.hasOwnProperty(i)){var a=o(i);if(e>=a[0]&&e<=a[1])return!0}}return!1}function v(e,t){var n=T[e];if(delete T[e],"undefined"!=typeof n&&n.isInQuery){var i=t?d.distance(t,j):null;r("key_exited",e,t,i)}}function m(e){c(e.key(),t(e.val()))}function p(e){c(e.key(),t(e.val()))}function y(e){var n=e.key();T.hasOwnProperty(n)&&I.child(n).once("value",function(e){var r=null===e.val()?null:t(e.val()),i=null!==r?O(r):null;f(i)||v(n,r)})}function g(e){var t=b.indexOf(e);t>-1&&b.splice(t,1),M=0===b.length,M&&i()}function w(){var e=Q(j,N).map(a);e=e.filter(function(t,n){return e.indexOf(t)===n});for(var t=Object.keys(x),n=t.length,r=0;n>r;++r){var i=t[r],l=e.indexOf(i);-1===l?x[i].active=!1:(x[i].active=!0,e.splice(l,1))}_===!1&&Object.keys(x).length>25&&(_=!0,C=setTimeout(u,10)),b=e.slice(),e.forEach(function(e){var t=o(e),n=I.orderByChild("geohash").startAt(t[0]).endAt(t[1]),r=n.on("child_added",m),i=n.on("child_removed",y),a=n.on("child_changed",p),l=n.on("value",function(){n.off("value",l),g(e)});x[e]={active:!0,childAddedCallback:r,childRemovedCallback:i,childChangedCallback:a,valueCallback:l}}),0===e.length&&g()}if(this.center=function(){return j},this.radius=function(){return N},this.updateCriteria=function(e){A(e),j=e.center||j,N=e.radius||N;for(var t=Object.keys(T),n=t.length,i=0;n>i;++i){var o=t[i];if(P===!0)break;var a=T[o],l=a.isInQuery;a.distanceFromCenter=d.distance(a.location,j),a.isInQuery=a.distanceFromCenter<=N,l&&!a.isInQuery?r("key_exited",o,a.location,a.distanceFromCenter):!l&&a.isInQuery&&r("key_entered",o,a.location,a.distanceFromCenter)}M=!1,w()},this.on=function(e,t){if(-1===["ready","key_entered","key_exited","key_moved"].indexOf(e))throw new Error('event type must be "ready", "key_entered", "key_exited", or "key_moved"');if("function"!=typeof t)throw new Error("callback must be a function");if(k[e].push(t),"key_entered"===e)for(var n=Object.keys(T),r=n.length,i=0;r>i;++i){var o=n[i],a=T[o];a.isInQuery&&t(o,a.location,a.distanceFromCenter)}return"ready"===e&&M&&t(),new l(function(){k[e].splice(k[e].indexOf(t),1)})},this.cancel=function(){P=!0,k={ready:[],key_entered:[],key_exited:[],key_moved:[]};for(var e=Object.keys(x),t=e.length,n=0;t>n;++n){var r=e[n],i=o(r);s(i,x[r]),delete x[r]}T={},clearInterval(L)},"[object Object]"!==Object.prototype.toString.call(e))throw new Error("wilddogRef must be an instance of Wilddog");var b,I=e,k={ready:[],key_entered:[],key_exited:[],key_moved:[]},P=!1,M=!1,T={},x={},_=!1,C=null,L=setInterval(function(){_===!1&&u()},1e4);A(n,!0);var j=n.center,N=n.radius;w()},u=function(e,n){function r(){var e,n=function(n,r){var l;n&&a?l=h.orderByChild("timestamp").startAt(n).endAt(a).limitToFirst(500):!n&&a?l=h.orderByChild("timestamp").endAt(a).limitToFirst(500):n&&!a?l=h.orderByChild("timestamp").startAt(n).limitToFirst(500):n||a||(l=h.orderByChild("timestamp").limitToFirst(500)),l.once("value",function(n){var i=n.numChildren()-1;n.forEach(function(e){if(i>1)if(i--,"remove"!==e.val().msg){var n,r=t(e.val());u.latestPoint&&(n=d.distance(u.latestPoint,r),u.length+=n),u.points.push(r),u.latestPoint=r;for(var o=new c(u),a=0;a<v.length;a++)v[a](o,null)}else u={points:[],length:0,latestPoint:null}});var l=u.points[u.points.length-1];if(e=l?l.timestamp():o,n.numChildren()>=500&&u.points[u.points.length-1].timestamp()<a)r(!1);else{for(var s=new c(u),h=0;h<m.length;h++)m[h](s,null),m.splice(h,1);f="success",r(!0)}},function(e){f="failed",i=e})},r=function(i){if(i){var o,l=function(e){var n,r=t(e.val());u.latestPoint&&(n=d.distance(u.latestPoint,r),u.length+=n),u.points.push(r),u.latestPoint=r;for(var i=new c(u),o=0;o<v.length;o++)v[o](i,null)};e&&a?o=h.orderByChild("timestamp").startAt(e).endAt(a):!e&&a?o=h.orderByChild("timestamp").endAt(a):e&&!a?o=h.orderByChild("timestamp").startAt(e):e||a||(o=h.orderByChild("timestamp")),o.on("child_added",l)}else n(e,r)};n(o,r)}x(n);var i,o=n.startTime||null,a=n.endTime||null,s=n.key,u={points:[],length:0,latestPoint:null},f="pending",h=e.child(s+"/points"),v=[],m=[],p=e.child(s+"/isExists");p.on("value",function(e){if(!e.val()){u={points:[],length:0,latestPoint:null};for(var t=new c(u),n=0;n<v.length;n++)v[n](t,null);for(var r=0;r<m.length;r++)m[r](t,null),m.splice(r,1)}}),this.startTime=function(){return o},this.endTime=function(){return a},this.key=function(){return s},this.on=function(e){switch(f){case"failed":return void e(null,i);case"closed":return void e(null,new Error("The query has been closed!"))}var t={points:[],length:0,latestPoint:null};if(u.points.length>0)for(var n=0;n<u.points.length;n++){var r,o=u.points[n];t.latestPoint&&(r=d.distance(t.latestPoint,o),t.length+=r),t.points.push(u.points[n]),t.latestPoint=o,e(new c(t))}return t=null,v.push(e),new l(function(){v.splice(v.indexOf(e),1)})},this.once=function(e){switch(f){case"success":e(new c(u),null);break;case"failed":e(null,i);break;case"pending":m.push(e);break;case"closed":e(null,new Error("The query has been closed!"))}},this.cancel=function(){f="closed",h.off(),v=[]},r()},c=function(e){var t=e.points,n=e.length,r=e.latestPoint;this.points=function(){return t},this.length=function(){return n},this.latestPoint=function(){return r}},f=function(e){var t=e.location[0],n=e.location[1],r=e.timestamp||Date.now(),i=e.customAttributes||null;this.latitude=function(){return t},this.longitude=function(){return n},this.timestamp=function(){return r},this.customAttributes=function(){return i}},d=function(n){var c=n.sync().ref("WilddogLocation"),d={};this.customPosition=function(e,t){M(e);var n={location:e,timestamp:t?t.timestamp:null,customAttributes:t?t.customAttributes:null};return new f(n)},this.setPosition=function(t,n){var r;if("string"==typeof t&&0!==t.length)r={},r[t]=n;else{if("object"!=typeof t)throw new Error("keyOrLocations must be a string or a mapping of key - location pairs.");if("undefined"!=typeof n)throw new Error("The location argument should not be used if you pass an object to set().");r=t}var i={};return Object.keys(r).forEach(function(t){k(t);var n=r[t];if(null===n)i[t]=null;else{E(n);var o=O(n),a=e(n,o);i[t]=a}}),c.child("map").update(i)};var h=function(t,n){var r,i={};if("object"!=typeof n||Array.isArray(n)){if("object"!=typeof n)throw new Error("The positions must be a position or a array of positions!");Array.isArray(n)||(r=n)}else r=[n];for(var o=0;o<r.length;o++){E(r[o]);var a=c.push().key(),l=O(r[o]),s=e(r[o],l);i["points/"+a]=s,i.isExists=!0}return c.child("path/"+t).update(i)};this.getPosition=function(e){return k(e),c.child("map/"+e).once("value").then(function(e){return null===e.val()?null:t(e.val())})},this.onPosition=function(e,n){k(e);var r=function(e){null===e.val()?n(null,null):n(t(e.val()),null)};return c.child("map/"+e).on("value",r,function(e){n(null,e)}),Array.isArray(d[e])?d[e].push(r):d[e]=[r],new l(function(){c.child("map/"+e).off("value",r);var t=d[e].indexOf(r);d[e].splice(t,1)})},this.offPosition=function(e){if(e)k(e),delete d[e],c.child("map/"+e).off("value");else for(var t in d)d.hasOwnProperty(t)&&(delete d[t],c.child("map/"+t).off("value"))},this.removePath=function(e){return k(e),c.child("path/"+e).remove()},this.removePosition=function(e){return k(e),this.setPosition(e,null)},this.circleQuery=function(e){return new s(c.child("map"),e)},this.pathQuery=function(e,t){var n={key:e,startTime:t?t.startTime:null,endTime:t?t.endTime:null};return new u(c.child("path"),n)};var v={};this.startTracingPosition=function(e,t){k(e);var n;t?(_(t),n=t):n="weapp"===i.type?this.WXLocationProvider("timeInterval",5e3):this.AMapLocationProvider("timeInterval",5e3);var r=this,o=function(t){t&&r.setPosition(e,t)};v[e]&&v[e].provider&&v[e].callback&&v[e].provider._off(v[e].callback),v[e]={provider:n,callback:o},v[e].provider._on(v[e].callback)},this.stopTracingPosition=function(e){if(k(e),!v[e]||!v[e].provider||!v[e].callback)throw new Error("The has not been startTraced or has been stoped!");v[e].provider._off(v[e].callback),delete v[e]};var m={};this.startRecordingPath=function(e,t){k(e);var n;t?(_(t),n=t):n="weapp"===i.type?this.WXLocationProvider("timeInterval",5e3):this.AMapLocationProvider("timeInterval",5e3);var r=function(t){t&&h(e,t)};m[e]&&m[e].provider&&m[e].callback&&m[e].provider._off(m[e].callback),m[e]={provider:n,callback:r},m[e].provider._on(m[e].callback)},this.stopRecordingPath=function(e){if(k(e),!m[e]||!m[e].provider||!m[e].callback)throw new Error("The has not been startTraced or has been stoped!");m[e].provider._off(m[e].callback),delete m[e]},this.AMapLocationProvider=function(e,t){if("weapp"===i.type)throw new Error("AMapLocationProvider is not weapp's method!");var n;if("timeInterval"===e){var r=parseInt(t);if(r>3e5||1e3>r)throw new Error("The timeInterval must in 1000~300000!");n={sampleType:"timeInterval",timeInterval:r}}else{if("distanceInterval"!==e)return new Error("IntervalType must be timeInterval or distanceInterval!");var a=parseFloat(t);if(a>500)throw new Error("The distanceInterval must in 0~500!");n={sampleType:"distanceInterval",distanceInterval:a}}return new o(n)},this.AMapWXLocationProvider=function(e,t,n){if("weapp"!==i.type)throw new Error("AMapWXLocationProvider is weapp's method!");if(!n)throw new Error("AMapWX is necessary param!");var r;if("timeInterval"===e){var o=parseInt(t);if(o>3e5||1e3>o)throw new Error("The timeInterval must in 1000~300000!");r={sampleType:"timeInterval",timeInterval:o,AMapWX:n}}else{if("distanceInterval"!==e)return new Error("IntervalType must be timeInterval or distanceInterval!");var l=parseFloat(t);if(l>500)throw new Error("The distanceInterval must in 0~500!");r={sampleType:"distanceInterval",distanceInterval:l,AMapWX:n}}return new a(r)},this.WXLocationProvider=function(e,t){if("weapp"!==i.type)throw new Error("AMapWXLocationProvider is weapp's method!");var n;if("timeInterval"===e){var r=parseInt(t);if(r>3e5||1e3>r)throw new Error("The timeInterval must in 1000~300000!");n={sampleType:"timeInterval",timeInterval:r}}else{if("distanceInterval"!==e)return new Error("IntervalType must be timeInterval or distanceInterval!");var o=parseFloat(t);if(o>500)throw new Error("The distanceInterval must in 0~500!");n={sampleType:"distanceInterval",distanceInterval:o}}return new B(n)};var p=function(){var e=n.options.syncURL.split("https://")[1].split(".wilddogio.com")[0],t=i.getPlatformInfo();t.appid=e,r("https://trace.wilddog.com/",t,"GET")["catch"](function(e){console.log(e)})};p()};d.distance=function(e,t){E(e),E(t);var n=6371,r=C(t.latitude()-e.latitude()),i=C(t.longitude()-e.longitude()),o=Math.sin(r/2)*Math.sin(r/2)+Math.cos(C(e.latitude()))*Math.cos(C(t.latitude()))*Math.sin(i/2)*Math.sin(i/2),a=2*Math.atan2(Math.sqrt(o),Math.sqrt(1-o));return Math.round(n*a*1e3)};var h=10,v="0123456789bcdefghjkmnpqrstuvwxyz",m=40007860,p=110574,y=5,g=22*y,w=6378137,b=.00669447819799,I=1e-12;Math.log2=Math.log2||function(e){return Math.log(e)/Math.log(2)};var k=function(e){var t;if("string"!=typeof e?t="key must be a string":0===e.length?t="key cannot be the empty string":1+h+e.length>755?t="key is too long to be stored in Wilddog":/[\[\].#$\/\u0000-\u001F\u007F]/.test(e)&&(t="key cannot contain any of the following characters: . # $ ] [ /"),"undefined"!=typeof t)throw new Error("Invalid WildLocation key '"+e+"': "+t)},E=function(e){var t;if("object"!=typeof e)t="location must be an object";else{var n=e.latitude(),r=e.longitude(),i=e.timestamp();"number"!=typeof n||isNaN(n)?t="latitude must be a number":-90>n||n>90?t="latitude must be within the range [-90, 90]":"number"!=typeof r||isNaN(r)?t="longitude must be a number":-180>r||r>180?t="longitude must be within the range [-180, 180]":("number"!=typeof n||isNaN(n)||1e10>i)&&(t="timestamp must be correct timestamp")}if("undefined"!=typeof t)throw new Error("Invalid WildLocation location '"+e+"': "+t)},P=function(e){var t;if("object"!=typeof e)t="location must be an object";else{var n,r,i;Array.isArray(e)?(n=e[0],r=e[1],i=Date.now()):(n=e.latitude(),r=e.longitude(),i=e.timestamp()),"number"!=typeof n||isNaN(n)?t="latitude must be a number":-90>n||n>90?t="latitude must be within the range [-90, 90]":"number"!=typeof r||isNaN(r)?t="longitude must be a number":-180>r||r>180?t="longitude must be within the range [-180, 180]":("number"!=typeof i||isNaN(i)||1e10>i)&&(t="timestamp must be correct timestamp")}if("undefined"!=typeof t)throw new Error("Invalid WildLocation location '"+e+"': "+t)},M=function(e){var t;if(Array.isArray(e)){var n=e[0],r=e[1];"number"!=typeof n||isNaN(n)?t="latitude must be a number":-90>n||n>90?t="latitude must be within the range [-90, 90]":"number"!=typeof r||isNaN(r)?t="longitude must be a number":(-180>r||r>180)&&(t="longitude must be within the range [-180, 180]")}else t="locationArray must be a array";if("undefined"!=typeof t)throw new Error("Invalid Wilddog.Location location '"+e+"': "+t)},T=function(e){var t;if("string"!=typeof e)t="geohash must be a string";else if(0===e.length)t="geohash cannot be the empty string";else for(var n=0,r=e.length;r>n;++n)-1===v.indexOf(e[n])&&(t='geohash cannot contain "'+e[n]+'"');if("undefined"!=typeof t)throw new Error("Invalid WildLocation geohash '"+e+"': "+t)},A=function(e,t){if("object"!=typeof e)throw new Error("query criteria must be an object");if("undefined"==typeof e.center&&"undefined"==typeof e.radius)throw new Error("radius and/or center must be specified");if(t&&("undefined"==typeof e.center||"undefined"==typeof e.radius))throw new Error("query criteria for a new query must contain both a center and a radius");for(var n=Object.keys(e),r=n.length,i=0;r>i;++i){var o=n[i];if("center"!==o&&"radius"!==o)throw new Error("Unexpected attribute '"+o+"'' found in query criteria")}if("undefined"!=typeof e.center&&E(e.center),"undefined"!=typeof e.radius){if("number"!=typeof e.radius||isNaN(e.radius))throw new Error("radius must be a number");if(e.radius<0)throw new Error("radius must be greater than or equal to 0")}},x=function(e){if("object"!=typeof e)throw new Error("query pathConstrains must be an object");if("string"!=typeof e.key)throw new Error("key must be specified!");if(e.startTime){var t=parseInt(e.startTime);if(1e10>t)throw new Error("startTime must be correct timestamp!")}if(e.endTime){var n=parseInt(e.endTime);if(1e10>n)throw new Error("endTime must be correct timestamp!")}for(var r=Object.keys(e),i=r.length,o=0;i>o;++o){var a=r[o];if("key"!==a&&"startTime"!==a&&"endTime"!==a)throw new Error("Unexpected attribute '"+a+"'' found in path constrains")}},_=function(e){if("object"!=typeof e)throw new Error("Invalid AMapLocationProvider object!");if("timeInterval"!=e.sampleType()&&"distanceInterval"!=e.sampleType())throw new Error("Invalid AMapLocationProvider.sampleType! SampleType must be timeInterval or distanceInterval!");if("number"!=typeof e.timeInterval()&&isNaN(e.timeInterval()))throw new Error("Error: timeInterval must be a number");if("number"!=typeof e.distanceInterval()&&isNaN(e.distanceInterval()))throw new Error("Error: distance must be a number")},C=function(e){if("number"!=typeof e||isNaN(e))throw new Error("Error: degrees must be a number");return e*Math.PI/180},O=function(e,t){if(P(e),"undefined"!=typeof t){if("number"!=typeof t||isNaN(t))throw new Error("precision must be a number");if(0>=t)throw new Error("precision must be greater than 0");if(t>22)throw new Error("precision cannot be greater than 22");if(Math.round(t)!==t)throw new Error("precision must be an integer")}t=t||h;for(var n={min:-90,max:90},r={min:-180,max:180},i="",o=0,a=0,l=1;i.length<t;){var s;s=Array.isArray(e)?l?e[1]:e[0]:l?e.longitude():e.latitude();var u=l?r:n,c=(u.min+u.max)/2;s>c?(o=(o<<1)+1,u.min=c):(o=(o<<1)+0,u.max=c),l=!l,4>a?a++:(a=0,i+=v[o],o=0)}return i},L=function(e,t){var n=C(t),r=Math.cos(n)*w*Math.PI/180,i=1/Math.sqrt(1-b*Math.sin(n)*Math.sin(n)),o=r*i;return I>o?e>0?360:0:Math.min(360,e/o)},j=function(e,t){var n=L(e,t);return Math.abs(n)>1e-6?Math.max(1,Math.log2(360/n)):1},N=function(e){return Math.min(Math.log2(m/2/e),g)},W=function(e){if(180>=e&&e>=-180)return e;var t=e+180;return t>0?t%360-180:180- -t%360},q=function(e,t){var n=t/p,r=Math.min(90,e.latitude()+n),i=Math.max(-90,e.latitude()-n),o=2*Math.floor(N(t)),a=2*Math.floor(j(t,r))-1,l=2*Math.floor(j(t,i))-1;return Math.min(o,a,l,g)},F=function(e,t){var n=t/p,r=Math.min(90,e.latitude()+n),i=Math.max(-90,e.latitude()-n),o=L(t,r),a=L(t,i),l=Math.max(o,a);return[[e.latitude(),e.longitude()],[e.latitude(),W(e.longitude()-l)],[e.latitude(),W(e.longitude()+l)],[r,e.longitude()],[r,W(e.longitude()-l)],[r,W(e.longitude()+l)],[i,e.longitude()],[i,W(e.longitude()-l)],[i,W(e.longitude()+l)]]},R=function(e,t){T(e);var n=Math.ceil(t/y);if(e.length<n)return[e,e+"~"];e=e.substring(0,n);var r=e.substring(0,e.length-1),i=v.indexOf(e.charAt(e.length-1)),o=t-r.length*y,a=y-o,l=i>>a<<a,s=l+(1<<a);return s>31?[r+v[l],r+"~"]:[r+v[l],r+v[s]]},Q=function(e,t){E(e);var n=Math.max(1,q(e,t)),r=Math.ceil(n/y),i=F(e,t),o=i.map(function(e){return R(O(e,r),n)});return o.filter(function(e,t){return!o.some(function(n,r){return t>r&&e[0]===n[0]&&e[1]===n[1]})})},B=function(e){var t=e&&e.sampleType||"timeInterval",n=e&&e.timeInterval||1e3,r=e&&e.distanceInterval||0;this.sampleType=function(){return t},this.timeInterval=function(){return n},this.distanceInterval=function(){return r};var i,o,a=[];this._on=function(e){a.push(e),i||(i=setInterval(function(){wx.getLocation({success:function(e){var n=null,i=null,l={};l.location=[e.latitude,e.longitude],i=new f(l);var s=function(){for(var e=0;e<a.length;e++)a[e](i,n)};if("timeInterval"==t)s();else if(i&&o){var u=d.distance(i,o);u>=r&&(o=i,s())}else o=i,s()},fail:function(e){var t=new Error(e.Msg),n=null,r=function(){for(var e=0;e<a.length;e++)a[e](n,t)};o=n,r()}})},n))},this._off=function(e){var t=a.indexOf(e);-1!=t&&a.splice(t,1),0===a.length&&(clearInterval(i),i=null)},this.getCurrentPosition=function(){return new Promise(function(e,t){wx.getLocation({success:function(t){var n={};n.location=[t.latitude,t.longitude];var r=new f(n);e(r)},fail:function(e){t(new Error(e.Msg))}})})}};return d}();"undefined"!=typeof module?module.exports=RealtimeLocation:"undefined"==typeof wilddog?console.error("wilddog is not exist"):(wilddog.regService("location",function(e){if(null===e)throw new Error("application not initialized!Please call wilddog.initializeApp first");return new RealtimeLocation(e)}),wilddog.Location=RealtimeLocation);