XWWitPark/witpark_jzzs/xw_jzzs_web/public/widgets/roamLine/widget.js
2024-09-12 08:10:00 +08:00

2 lines
8.2 KiB
JavaScript

/* 2020-9-24 14:48:24 | 版权所有 军懋国兴 */
mars3d.widget.bindClass(mars3d.widget.BaseWidget.extend({options:{view:{type:"window",url:"view.html",windowOptions:{width:250,position:{top:50,right:5,bottom:5}}}},drawControl:null,create:function(){this.drawControl=new mars3d.Draw(this.viewer,{hasEdit:!0});var e=this;this.drawControl.on(mars3d.draw.event.DrawCreated,function(t){e.viewWindow&&e.viewWindow.plotlist.plotEnd()}),this.drawControl.on(mars3d.draw.event.EditStart,function(t){var i=t.entity;e.startEditing(i)}),this.drawControl.on(mars3d.draw.event.EditMovePoint,function(t){var i=t.entity;e.startEditing(i)}),this.drawControl.on(mars3d.draw.event.EditRemovePoint,function(t){var i=t.entity;e.startEditing(i)}),this.drawControl.on(mars3d.draw.event.EditStop,function(t){var i=t.entity;e.stopEditing(i),e.saveEntity(i)}),this.getList()},viewWindow:null,winCreateOK:function(t,i){this.viewWindow=i},activate:function(){this.drawControl.hasEdit(!0),this.drawControl.setVisible(!0)},disable:function(){this.viewWindow=null,this.drawControl.stopDraw(),this.lastEditEntity&&(this.lastEditEntity._polyline.show=!1,this.lastEditEntity._attribute.style.show=!1,this.lastEditEntity=null),this.drawControl.hasEdit(!1),this.drawControl.setVisible(!1)},startDraw:function(t){this.lastEditEntity&&(this.lastEditEntity._polyline.show=!1,this.lastEditEntity._attribute.style.show=!1,this.lastEditEntity=null),this.drawControl.startDraw(t)},startEditingById:function(t){var i=this.drawControl.getEntityById(t);null!=i&&(this.viewer.flyTo(i),this.drawControl.startEditing(i))},lastEditEntity:null,startEditing:function(t){this.lastEditEntity&&(this.lastEditEntity._polyline.show=!1,this.lastEditEntity._attribute.style.show=!1,this.lastEditEntity=null),(this.lastEditEntity=t)._polyline.show=!0,t._attribute.style.show=!0;var i=this.drawControl.getCoordinates(t);this.viewWindow.plotEdit.startEditing(t.attribute,i)},stopEditing:function(){this.viewWindow&&this.viewWindow.plotEdit.stopEditing()},stopDraw:function(){this.drawControl.stopDraw()},updateAttr2map:function(t){this.drawControl.updateAttribute(t)},updateGeo2map:function(t,i){var e=[];if(i)for(var a=0;a<t.length;a+=3){var r=Cesium.Cartesian3.fromDegrees(t[a],t[a+1],t[a+2]);e.push(r)}else for(a=0;a<t.length;a+=2){r=Cesium.Cartesian3.fromDegrees(t[a],t[a+1],0);e.push(r)}return this.drawControl.setPositions(e),e.length<=3&&this.centerCurrentEntity(),e},centerCurrentEntity:function(){var t=this.drawControl.getCurrentEntity();null!=t&&this.viewer.flyTo(t)},getGeoJson:function(){return this.drawControl.toGeoJSON()},jsonToLayer:function(t,i,e){if(null!=t)return this.drawControl.loadJson(t,{clear:i,flyTo:e})},deleteAll:function(){this.drawControl.deleteAll(),this.deleteAllData()},deleteEntity:function(t){var i=this.drawControl.getEntityById(t);null!=i&&(this.delEntity(t),this.drawControl.deleteEntity(i))},deleteCurrentEntity:function(){var t=this.drawControl.getCurrentEntity();null!=t&&(this.delEntity(t._attribute.attr.id),this.drawControl.deleteEntity(t))},hasEdit:function(t){this.drawControl.hasEdit(t)},storageName:"marsgis_roam",arrFlyTable:[],getList:function getList(){var that=this,laststorage,that;window.hasServer?sendAjax({url:"v1/map/flyroute/list",data:{userId:haoutil.storage.get("userId")},type:"get",dataType:"json",contentType:"application/x-www-form-urlencoded",success:function(t){for(var i=0;i<t.length;i++){var e=JSON.parse(t[i].geojson);e.id=t[i].id,that.arrFlyTable.push(e)}that.showData(that.arrFlyTable);that.viewWindow&&that.viewWindow.tableWork.loadData(that.arrFlyTable)}}):(laststorage=haoutil.storage.get(this.storageName),null!=laststorage&&(this.arrFlyTable=eval(laststorage)),null==this.arrFlyTable||0==this.arrFlyTable.length?(this.arrFlyTable=[],that=this,$.getJSON(this.path+"data/fly.json",function(t){that.arrFlyTable=that.arrFlyTable.concat(t),that.showData(that.arrFlyTable),that.viewWindow&&that.viewWindow.tableWork.loadData(that.arrFlyTable)})):(this.showData(this.arrFlyTable),this.viewWindow&&this.viewWindow.tableWork.loadData(this.arrFlyTable)))},showData:function(t){for(var i=[],e=0;e<t.length;e++){var a=t[e];a.properties.style&&(a.properties.style.show=!1);var r={type:"Feature",properties:a.properties,geometry:a.geometry};r.properties.attr.id=a.id,r.properties.attr.name=a.name,i.push(r)}return this.drawControl.loadJson({type:"FeatureCollection",features:i},{clear:!0,flyTo:!1})},deleteAllData:function(){this.arrFlyTable=[],haoutil.storage.add(this.storageName,JSON.stringify(this.arrFlyTable)),this.isActivate&&null!=this.viewWindow&&this.viewWindow.tableWork.loadData(this.arrFlyTable)},isOnDraw:!1,delEntity:function(t){if(t){this.drawControl.stopDraw(),window.hasServer&&sendAjax({url:"v1/map/flyroute/"+t,type:"delete",dataType:"json",contentType:"application/json",success:function(t){console.log("删除漫游路线成功,返回数据:"+JSON.stringify(t))},error:function(t){alert("服务出错:"+t.statusText+",代码 "+t.status)}});for(var i=this.arrFlyTable.length-1;0<=i;i--)if(this.arrFlyTable[i].id==t){this.arrFlyTable.splice(i,1);break}haoutil.storage.add(this.storageName,JSON.stringify(this.arrFlyTable)),this.isActivate&&null!=this.viewWindow&&this.viewWindow.tableWork.loadData(this.arrFlyTable)}else this.isOnDraw=!0},saveEntity:function(i){if(this.isOnDraw)this.isOnDraw=!1;else{var e=this,t=(new Date).format("MMddHHmmss"),a=!1;null!=i._attribute.attr.id&&""!=i._attribute.attr.id||(a=!0,i._attribute.attr.id=t),null!=i._attribute.attr.name&&""!=i._attribute.attr.name||(i._attribute.attr.name="路线"+t);var r=this.drawControl.toGeoJSON(i);if(!(r.geometry.coordinates.length<2)){var o={id:r.properties.attr.id,name:r.properties.attr.name,geometry:r.geometry,properties:r.properties};if(window.hasServer)o=JSON.stringify(o),a?sendAjax({url:"v1/map/flyroute/add",data:JSON.stringify({name:i._attribute.attr.name,geojson:o,remark:i._attribute.attr.remark}),type:"post",dataType:"json",contentType:"application/json",success:function(t){i._attribute.attr.id=t.id,(o=JSON.parse(o)).id=t.id,e.arrFlyTable.push(o),e.isActivate&&null!=e.viewWindow&&e.viewWindow.tableWork.loadData(e.arrFlyTable)}}):sendAjax({url:"v1/map/flyroute/update",data:JSON.stringify({name:i._attribute.attr.name,geojson:o,id:i._attribute.attr.id,remark:i._attribute.attr.remark}),type:"post",dataType:"json",contentType:"application/json",success:function(t){console.log("修改漫游成功,返回数据:"+JSON.stringify(t))}});else{for(var s=!1,n=this.arrFlyTable.length-1;0<=n;n--)if(this.arrFlyTable[n].id==o.id){s=!0,this.arrFlyTable[n]=o;break}s||this.arrFlyTable.push(o),haoutil.storage.add(this.storageName,JSON.stringify(this.arrFlyTable)),this.isActivate&&null!=this.viewWindow&&this.viewWindow.tableWork.loadData(this.arrFlyTable)}}}},toRoamFly:function(t){var i=this.getFormatData(t);mars3d.widget.activate({uri:"widgets/roamFly/widget.js",data:i})},saveForGeoJson:function(t){var i=this.getFormatData(t);haoutil.file.downloadFile(i.name+".json",JSON.stringify(i))},saveForCzml:function(t){var i,e,a;t.geometry.coordinates.length<2?toastr.error("路线无坐标数据,无法生成!"):(i=this.getFormatData(t),e=new mars3d.FlyLine(this.viewer,i),a=JSON.stringify(e.toCZML()),e.destroy(),haoutil.file.downloadFile(t.properties.attr.name+".czml",a))},getFormatData:function(t){var i=t.properties.attr,e={id:i.id,name:i.name,remark:i.remark,clockLoop:i.clockLoop,camera:{type:i.cameraType,followedX:i.followedX,followedZ:i.followedZ},showGroundHeight:i.showGroundHeight,clampToGround:i.clampToGround,interpolation:i.interpolation,points:t.geometry.coordinates,speed:t.properties.speed,model:this.getModelCfg(i.model)};return i.showLabel&&(e.label={show:!0}),i.showLine&&(e.path=t.properties.style,e.path.show=!0),i.showShadow&&(e.shadow=[{show:!0,type:i.shadowType}]),e},getModelCfg:function(t){switch(t){case"model_man":return{show:!0,uri:serverURL_gltf+"/mars/man/walk.gltf",scale:1,minimumPixelSize:30};case"model_car":return{show:!0,uri:serverURL_gltf+"/mars/qiche.gltf",scale:.2,minimumPixelSize:50};case"model_air":return{show:!0,uri:serverURL_gltf+"/mars/feiji.glb",scale:.1,minimumPixelSize:50};case"model_zhanji":return{show:!0,uri:serverURL_gltf+"/mars/zhanji.glb",scale:.01,minimumPixelSize:50};case"model_weixin":return{show:!0,uri:serverURL_gltf+"/mars/weixin.gltf",scale:1,minimumPixelSize:100}}return{show:!1}}}));