-
Notifications
You must be signed in to change notification settings - Fork 183
/
geomap.js
1 lines (1 loc) · 4.07 KB
/
geomap.js
1
define("geomap/geomap/1.0.0/geomap",["gallery/jquery/1.8.3/jquery","gallery/raphael/2.1.0/raphael"],function(a,b,c){var d=a("gallery/jquery/1.8.3/jquery"),e=a("gallery/raphael/2.1.0/raphael");(function(){var a=function(a,b,c){var f=this;f.container=d(a),f.width=b||f.container.width(),f.height=c||f.container.height(),f.viewBox={a:0,b:0,w:f.width,h:f.height},f.canvas=new e(f.container.get(0),f.width,f.height),f.mapPaths={},f.offset={},f.scale={},f.defaultConfig={srcPath:"",scale:{x:1,y:1},translate:{x:0,y:0},style:{fill:"#CFEBF7",stroke:"#fff","stroke-width":1,"stroke-linejoin":"round"},loadingTxt:{loading:"载入数据",fail:"数据载入失败",fill:"#333"}}};a.prototype={showLoadingTip:function(a){var d,e,f,g,b=this,c=b.canvas;b.removeLoadingTip(),d=c.text(b.width/2,b.height/2,a).attr({fill:"#fff","font-size":"12px"}),d.id="loadingTip",f=d.node.clientWidth,g=d.node.clientHeight,e=c.rect((b.width-f)/2,(b.height-g)/2,f,g).attr({fill:"#000"}),e.insertBefore(d),e.id="loadingTipBox"},removeLoadingTip:function(){var a=this,b=a.canvas.getById("loadingTip"),c=a.canvas.getById("loadingTipBox");b&&(b.remove(),c.remove())},render:function(){var c,e,f,g,a=this,b=a.canvas,h=function(){},i={};1==arguments.length?(g={}.toString.call(arguments[0]),"[object Object]"==g?i=arguments[0]:"[object Function]"==g&&(h=arguments[0])):2==arguments.length&&(i=arguments[0],h=arguments[1]),b.clear(),a.mapPaths={},d.extend(!0,a.defaultConfig,i),i=a.defaultConfig,a.showLoadingTip("载入数据..."),c=d.ajax({url:i.srcPath,dataType:"json"}).done(function(c){if(b.clear(),!c.offset)for(var l,m,n,o,g=c.features,j=180,k=0,p=0,q=g.length;q>p;p++){l=g[p].properties.name,m=g[p].geometry,n=m.coordinates;for(var r=0,s=n.length;s>r;r++){"Polygon"==m.type?o=n[r]:"MultiPolygon"==m.type&&(o=n[r][0]);for(var t=0,u=o.length;u>t;t++)j>o[t][0]&&(j=o[t][0]),o[t][1]>k&&(k=o[t][1])}c.offset={x:-j,y:k}}c.offset.x+=i.translate.x,c.offset.y+=i.translate.y,a.offset=c.offset,a.scale=i.scale,c=a.formatGeoJSON(c),e=a.scale.x,f=a.scale.y,d.each(c,function(c,e){var f=b.path(e).attr(i.style);d(f.node).attr({"class":"map-path","path-name":c}),a.mapPaths[c]=f}),a.dragMove(),h()}).fail(function(){b.clear(),a.showLoadingTip("载入失败!")})},dragMove:function(){function h(a){e=!0,f=a.clientX,g=a.clientY,d(this).css("cursor","move")}function i(){e=!1,d(this).css("cursor","default")}function j(c){function d(a,b,c,d,e){var f=a-(b-c)/2;return 0>=f||f>=d-e?a:f}return e?(b.a=d(b.a,c.clientX,f,a.width,b.w),b.b=d(b.b,c.clientY,g,a.height,b.h),f=c.clientX,g=c.clientY,a.canvas.setViewBox(b.a,b.b,b.w,b.h),void 0):!1}var a=this,b=a.viewBox,c=d(a.canvas.canvas),e=!1,f=0,g=0;c.on("mousedown",h).on("mouseup",i).on("mousemove",j)},viewScale:function(a){var b=this,c=b.viewBox,d=c.w,e=c.h,f=c.a,g=c.b,h=1;switch(a){case"up":h+=.2;break;case"down":h-=.2;break;default:return!1}c.w=c.w/h,c.h=c.h/h,c.a=c.a+(d-c.w)/2,c.b=c.b+(e-c.h)/2,h>1&&c.w<b.width/2?(c.w=d,c.h=e,c.a=f,c.b=g):1>h&&c.w>b.width&&(c.w=b.width,c.h=b.height,c.a=0,c.b=0),0>c.a&&(c.a=0),0>c.b&&(c.b=0),c.a>b.width-c.w&&(c.a=b.width-c.w),c.b>b.width-c.h&&(c.b=b.height-c.h),b.canvas.setViewBox(c.a,c.b,c.w,c.h)},setPoint:function(a){var e,f,g,b=this,c={x:0,y:0,r:1,opacity:.5,fill:"#238CC3",stroke:"#238CC3","stroke-width":0,"stroke-linejoin":"round"};return d.extend(!0,c,a),e=(c.x+b.offset.x)*b.scale.x,f=(b.offset.y-c.y)*b.scale.y,g=b.canvas.circle(e,f,c.r).attr(c)},setLine:function(a){var f,g,h,b=this,c={ps:[],stroke:"#238CC3","stroke-width":.5,"stroke-linejoin":"round"},e=[];d.extend(!0,c,a);for(var i=0,j=c.ps.length;j>i;i++)f=(1*c.ps[i].x+b.offset.x)*b.scale.x,g=(b.offset.y-1*c.ps[i].y)*b.scale.y,e.push(f+","+g);return e="M"+e.join("L"),h=b.canvas.path(e).attr(c)},formatGeoJSON:function(a){for(var g,h,i,j,b=this,c=a.features,d=a.offset.x,e=a.offset.y,f={},k=0,l=c.length;l>k;k++){g=c[k].properties.name,h=c[k].geometry,i=h.coordinates;for(var m=0,n=i.length;n>m;m++){"Polygon"==h.type?j=i[m]:"MultiPolygon"==h.type&&(j=i[m][0]);for(var o=0,p=j.length;p>o;o++)-168.5>j[o][0]&&(j[o][0]=j[o][0]+360),j[o]=(j[o][0]+d)*b.scale.x+","+(e-j[o][1])*b.scale.y;i[m]="M"+j.join("L")+"z"}f[g]=i.join("")}return f}},c.exports=a})()});