geomapping
Version:
Geolocation coordinates on image
3 lines (2 loc) • 5.21 kB
JavaScript
console.log("geomapping v0.0.2");class t{static PIXELS_OFFSET=268435456;static PIXELS_RADIUS=85445659.4471;static EVENTS={READY:"ready",ADD:"add",REMOVE:"remove",UPDATE_POSITION:"update_position",STOP_POSITION:"stop_position"};_events={};_eventsOnce=[];_bbox=void 0;_points=[];_img=void 0;_watchPositionId=void 0;constructor(t){if(this._config=t,this._config.boundingBox)return"boolean"!=typeof this._config.testOutOfImage&&(this._config.testOutOfImage=!1),this._init(),this;console.error("GeoMapping :: boundingBox is mandatory.")}_init(){this._generateEvents();let i=()=>{this._bbox=this._getStaticMapBoundingBoxFromTwoPoints(this._config.boundingBox[0],this._config.boundingBox[1]),this._config.points&&this._config.points.length>0&&this._config.points.forEach((t=>{this._addPoint(t.lat,t.lng,t.id,t.data)})),this.trigger(t.EVENTS.READY)};"string"==typeof this._config.imgSrc?this._preloadImg(this._config.imgSrc,(t=>{this._img=t,i()}),(t=>{console.error("GeoMapping :: error on preloading "+this._config.imgSrc,t)})):this._config.imgSrc?(this._img=this._config.imgSrc,i()):i()}_generateEvents=()=>{Object.keys(t.EVENTS).forEach((i=>{this._events[t.EVENTS[i]]=[]}))};_preloadImg(t,i,o){let n=new Image;n.onload=()=>{"function"==typeof i&&i(n)},n.onerror=t=>{"function"==typeof o&&o(t)},n.src=t}_lngToX(i){return Math.round(t.PIXELS_OFFSET+t.PIXELS_RADIUS*i*Math.PI/180)}_latToY(i){return Math.round(t.PIXELS_OFFSET-t.PIXELS_RADIUS*Math.log((1+Math.sin(i*Math.PI/180))/(1-Math.sin(i*Math.PI/180)))/2)}_XToLon(i){return(Math.round(i)-t.PIXELS_OFFSET)/t.PIXELS_RADIUS*180/Math.PI}_YToLat(i){return 180*(Math.PI/2-2*Math.atan(Math.exp((Math.round(i)-t.PIXELS_OFFSET)/t.PIXELS_RADIUS)))/Math.PI}_getStaticMapBoundingBoxFromTwoPoints(t,i){let o={tl:void 0,br:void 0,width:void 0,height:void 0},n={v_scale:void 0,h_scale:void 0,topLeft:void 0,bottomRight:void 0};o.tl={lat:this._latToY(Math.max(t.lat,i.lat)),lng:this._lngToX(Math.min(t.lng,i.lng)),x:Math.min(t.x,i.x),y:Math.min(t.y,i.y)},o.br={lat:this._latToY(Math.min(t.lat,i.lat)),lng:this._lngToX(Math.max(t.lng,i.lng)),x:Math.max(t.x,i.x),y:Math.max(t.y,i.y)},o.width=o.br.x-o.tl.x,o.height=o.br.y-o.tl.y;let e=o.br.lat-o.tl.lat,s=o.br.lng-o.tl.lng;return n.v_scale=e/o.height,n.h_scale=s/o.width,n.topLeft={x:o.tl.lng-o.tl.x*n.h_scale,y:o.tl.lat-o.tl.y*n.v_scale},n.topLeft.lat=this._YToLat(n.topLeft.y),n.topLeft.lng=this._XToLon(n.topLeft.x),n.bottomRight={x:o.br.lng+(o.width-o.br.x)*n.h_scale,y:o.br.lat+(o.height-o.br.y)*n.v_scale},n.bottomRight.lat=this._YToLat(n.bottomRight.y),n.bottomRight.lng=this._XToLon(n.bottomRight.x),n}_getPointXYOnStaticMap(t,i,o){return{lat:t,lng:i,x:(this._lngToX(i)-o.topLeft.x)/o.h_scale,y:(this._latToY(t)-o.topLeft.y)/o.v_scale}}_getPointLatLongOnStaticMap(t,i,o){return{lat:this._YToLat(o.topLeft.y+i*o.v_scale),lng:this._XToLon(o.topLeft.x+t*o.h_scale)}}_addPoint(t,i,o,n){let e=this._getPointXYOnStaticMap(t,i,this._bbox);return e.id=o,e.data=n,e.isOutOfMap=this._isOutOfImage(e.x,e.y),this._points.push(e),e}_isOutOfImage(t,i){return!!this._img&&!(t>=0&&t<=this._img.width&&i>=0&&i<=this._img.height)}getPoints(){return this._points}getPointById(t){return this._points.find((i=>i.id===t))}filterPointsByData(t,i){return this._points.filter((o=>o.data&&o.data[t]&&o.data[t]==i))}addPoint(i,o,n,e){let s=this._addPoint(i,o,n,e);return this.trigger(t.EVENTS.ADD,s),s}removePoint(i){this._points.forEach(((o,n)=>{o.id===i&&(this._points.splice(n,1),this.trigger(t.EVENTS.REMOVE,o))}))}getLatLngOnMap(t,i){if(!this._config.testOutOfImage||!this._isOutOfImage(t,i))return this._getPointLatLongOnStaticMap(t,i,this._bbox);console.error("GeoMapping :: this point is out of map.")}getPointXYOnMap(t,i){return this._getPointXYOnStaticMap(t,i,this._bbox)}trackPosition(i){if(!window.navigator||!window.navigator.geolocation)return console.error("GeoMapping :: error on trackPosition :: geolocation unavailable"),void("function"==typeof i&&i("GeoMapping :: error on trackPosition :: geolocation unavailable"));this._watchPositionId?console.warn("GeoMapping :: already tracking position"):this._watchPositionId=navigator.geolocation.watchPosition((i=>{this.trigger(t.EVENTS.UPDATE_POSITION,this._getPointXYOnStaticMap(i.coords.latitude,i.coords.longitude,this._bbox))}),(t=>{console.error("GeoMapping :: error on trackPosition",t),"function"==typeof i&&i("GeoMapping :: error on trackPosition",t)}))}stopTrackPosition(){this._watchPositionId?(navigator.geolocation.clearWatch(this._watchPositionId),this._watchPositionId=void 0,this.trigger(t.EVENTS.STOP_POSITION)):console.warn("GeoMapping :: tracking position is not activated")}on=(t,i)=>{this._events[t]&&this._events[t].push(i)};one=(t,i)=>{this._events[t]&&(this._events[t].push(i),this._eventsOnce.push(i))};off=(t,i)=>{this._events[t]&&-1!==this._events[t].indexOf(i)&&this._events[t].splice(this._events[t].indexOf(i),1),-1!==this._eventsOnce.indexOf(i)&&this._eventsOnce.splice(this._eventsOnce.indexOf(i),1)};trigger=(t,i)=>{this._events[t]&&this._events[t].forEach(((o,n)=>{o(i),-1!==this._eventsOnce.indexOf(o)&&(this._events[t].splice(n,1),this._eventsOnce.splice(this._eventsOnce.indexOf(o),1))}))}}export default t;
//# sourceMappingURL=geomapping.es6.min.js.map