cesium 官網的api文檔介紹地址cesium官網api,裏面詳細的介紹 cesium 各個類的介紹,還有就是在線例子:cesium 官網在線例子,這個也是學習 cesium 的好素材。git
1.cesium結合geoserver利用WFS服務實現圖層新增功能
2.源代碼demo下載web
效果圖以下:json
本篇主要是cesium經過調用geoserver發佈的地圖服務WFS來達到圖層新增記錄的目的。與GeoServer的WFS進行基於Rest交互關鍵就在於請求參數,值得注意的是這些請求最好採用POST方法發送。查詢能夠採用json,但增長,刪除,修改都只能採用XML形式Transactioncanvas
geoserver默認WFS服務是沒有編輯操做權限的,因此須要在geoserver設置權限,容許編輯操做才行,截圖以下:
api
部分核心代碼:app
//繪製geojson圖層樣式 var geoJsonStyle = { stroke: Cesium.Color.YELLOW, strokeWidth: 3, fill: Cesium.Color.YELLOW.withAlpha(0.1) }; var geoserverUrl = 'http://localhost:8080/geoserver/WebGIS'; var image_Source = new Cesium.UrlTemplateImageryProvider({ //url: 'http://mt0.google.cn/vt/lyrs=t,r&hl=zh-CN&gl=cn&x={x}&y={y}&z={z}', //url: 'https://server.arcgisonline.com/arcgis/rest/services/World_Imagery/MapServer/tile/{z}/{y}/{x}', url: "http://map.geoq.cn/ArcGIS/rest/services/ChinaOnlineStreetPurplishBlue/MapServer/tile/{z}/{y}/{x}", //tilingScheme : new Cesium.GeographicTilingScheme(), credit: '' }); var viewer = new Cesium.Viewer('map', { geocoder: false, homeButton: false, sceneModePicker: false, fullscreenButton: false, vrButton: false, baseLayerPicker: false, infoBox: false, selectionIndicator: false, animation: false, timeline: false, shouldAnimate: true, navigationHelpButton: false, navigationInstructionsInitiallyVisible: false, imageryProvider: image_Source }); //加載geoserver wms服務 var wms = new Cesium.WebMapServiceImageryProvider({ url: geoserverUrl+'/wms', layers: 'WebGIS:testLayer', parameters: { service : 'WMS', format: 'image/png', transparent: true, } }); viewer.imageryLayers.addImageryProvider(wms); viewer._cesiumWidget._creditContainer.style.display = "none"; viewer.scene.globe.enableLighting = false; //viewer.scene.globe.depthTestAgainstTerrain = true; viewer.scene.globe.showGroundAtmosphere = false; viewer.camera.flyTo({ destination : Cesium.Cartesian3.fromDegrees(113.90271877, 22.95186415,30000.0) }); var handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas); var ellipsoid = viewer.scene.globe.ellipsoid; handler.setInputAction(function (movement) { //經過指定的橢球或者地圖對應的座標系,將鼠標的二維座標轉換爲對應橢球體三維座標 cartesian = viewer.camera.pickEllipsoid(movement.position, ellipsoid); if (cartesian) { //將笛卡爾座標轉換爲地理座標 var cartographic = ellipsoid.cartesianToCartographic(cartesian); //將弧度轉爲度的十進制度表示 var longitudeString = Cesium.Math.toDegrees(cartographic.longitude); var latitudeString = Cesium.Math.toDegrees(cartographic.latitude); var point = longitudeString + ',' + latitudeString; queryByPoint(point,'testLayer',callbackLastQueryWFSService); } }, Cesium.ScreenSpaceEventType.LEFT_CLICK); var drawTool = new DrawTool({ viewer: viewer, hasEdit: false }); //繪製矩形 $("#rect_btn").click(function(){ //clearMap() if (!drawTool) return; drawTool.startDraw({ type: "rectangle", style: { heightReference: Cesium.HeightReference.CLAMP_TO_GROUND, //material:Cesium.Color.WHITE material:Cesium.Color.fromRgba(0x67ADDFFF) }, success: function (evt) { //console.log('evt',evt); var leftup = evt.leftup; var rightdown = evt.rightdown; //世界座標轉地理座標(弧度) var leftupcartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(leftup); var rightdowncartographic = viewer.scene.globe.ellipsoid.cartesianToCartographic(rightdown); //console.log('leftupcartographic',leftupcartographic); //地理座標(弧度)轉經緯度座標 var leftuppoint = [leftupcartographic.longitude / Math.PI * 180, leftupcartographic.latitude / Math.PI * 180]; console.log('leftuppoint',leftuppoint); var rightdownpoint = [rightdowncartographic.longitude / Math.PI * 180, rightdowncartographic.latitude / Math.PI * 180]; console.log('rightdown',rightdown); var extent = [leftuppoint[0].toFixed(6),leftuppoint[1].toFixed(6),rightdownpoint[0].toFixed(6),rightdownpoint[1].toFixed(6)]; var polygon = null; if(extent && extent.length>0){ //構造polygon polygon = ''; polygon += extent[0] + ',' + extent[1] + ' ' ; polygon += extent[2] + ',' + extent[1] + ' ' ; polygon += extent[2] + ',' + extent[3] + ' ' ; polygon += extent[0] + ',' + extent[3] + ' ' ; polygon += extent[0] + ',' + extent[1] + ' ' ; } console.log('polygon',polygon); if(polygon){ var content = '<span>名稱:</span><input type="text" id="estate_num" /></br><span>備註:</span><input type="text" id="holder_nam" /></br><button type="button" id="addBtn">新增</button>'; $("#infowindow").show(); $("#infowindow").empty(); $("#infowindow").append(content); $("#addBtn").click(function(){ console.log('點擊事件響應'); addLayers(polygon,$("#estate_num").val(),$("#holder_nam").val(),callbackAddLayersWFSService); }); } } }); });
/*圖層新增 *@method addLayers *@param polygon 圖形 *@param fieldValue1 字段1值 *@param fieldValue2 字段2值 *@return callback */ function addLayers(polygon,fieldValue1,fieldValue2, callback){ var xml = '<wfs:Transaction service="WFS" version="1.0.0" xmlns:opengis="http://webgis.com" xmlns:wfs="http://www.opengis.net/wfs" xmlns:ogc="http://www.opengis.net/ogc" xmlns:gml="http://www.opengis.net/gml" xmlns:xsi="http://www.w3.org/2001/XMLSchema-instance" xsi:schemaLocation="http://www.opengis.net/wfs http://schemas.opengis.net/wfs/1.0.0/WFS-basic.xsd">'; ……
更多詳情見下面連接文章:ide
小專欄此文章函數
文章提供源碼,對本專欄感興趣的話,能夠關注一波學習