viewer_spatialquery.vue 8.93 KB
<!--
 * 1、地图分屏效果
 * @Author: jiangbotao
 * @Date: 2019-12-03 22:52:56
 * @LastEditors: jiangbotao
 * @LastEditTime: 2019-12-07 23:25:28
 * @FilePath: \superglobevue\src\components\viewer.vue
 -->
<template>
    <div>
		<div id="cesiumContainer" v-bind:style="styleObject"></div>
		<div id='loadingbar' class="spinner">
			<div class="spinner-container container1">
				<div class="circle1"></div>
				<div class="circle2"></div>
				<div class="circle3"></div>
				<div class="circle4"></div>
			</div>
			<div class="spinner-container container2">
				<div class="circle1"></div>
				<div class="circle2"></div>
				<div class="circle3"></div>
				<div class="circle4"></div>
			</div>
			<div class="spinner-container container3">
				<div class="circle1"></div>
				<div class="circle2"></div>
				<div class="circle3"></div>
				<div class="circle4"></div>
			</div>
		</div>
        <div id="toolbar" class="param-container tool-bar">
			<div>
				<button id="search" >查询</button>
			</div>
		</div>
	</div>
</template>

<script>
import URL_CONFIG from "./../config/urlConfig.vue";
const Cesium = window.Cesium;
const SuperMap = window.SuperMap;
export default {
    data: function() {
        return {
            styleObject: {
                width: "100%",
                position: "absolute",
                top: "0px",
                bottom: "0px",
                left: "0px",
                backgroundColor: "#000000"
            },
            smviewer: {}
        };
    },
    mounted: function() {
        var __this = this;
        __this.viewer = new Cesium.Viewer('cesiumContainer', { animation: true });
        var scene = __this.viewer.scene;
		var widget = __this.viewer.cesiumWidget;
        // 隐藏logo
        $(".cesium-widget-credits")[0].style.visibility = "hidden";
        $(".cesium-viewer-animationContainer")[0].style.visibility="hidden";
        // 隐藏导航工具
        // $(".cesium-viewer-navigationContainer")[0].style.visibility="hidden";

        // 添加OSM地图
        let osm = Cesium.createOpenStreetMapImageryProvider({
            url: "https://tile-a.openstreetmap.fr/hot",
            fileExtension:"png"
        });
         __this.viewer.imageryLayers.addImageryProvider(osm);

        try {
            var groundPromise = scene.addS3MTilesLayerByScp(URL_CONFIG.SCP_CBD_GROUND1, {
                name: 'ground'
            });
            var buildPromise = scene.addS3MTilesLayerByScp(URL_CONFIG.SCP_CBD_BUILD, {
                name: 'build'
            });
            Cesium.when([groundPromise, buildPromise], function(layers) {
                scene.camera.setView({
                    destination : Cesium.Cartesian3.fromDegrees(116.4566, 39.9149, 5323.445971240632),
                    orientation : {
                        heading : 3.1612,
                        pitch : -1.5188,
                        roll : 6.283185307179563
                    }
                });
                var positions = null;
				// 绘制面
                var handlerPolygon = new Cesium.DrawHandler(__this.viewer, Cesium.DrawMode.Polygon, 0);
				handlerPolygon.drawEvt.addEventListener(function(polygon){
                    var points = polygon.object.positions;
                    var arr = [];
                    if(points && points instanceof Array && points.length >= 3){
                        for(var i = 0, j = points.length; i < j; i++){
                            var point = __this.convertPoint(points[i]);
                            if(point){
                                arr.push(point);
                            }
                        }
                    }
                    var linearRing = new SuperMap.Geometry.LinearRing(arr);
                    var searchPolygon = new SuperMap.Geometry.Polygon(linearRing);
                    query(searchPolygon);
                });
                $("#search").on("click", function(){
					handlerPolygon.activeEvt.addEventListener(function(isActive){
						if(isActive === true){
							__this.viewer.enableCursorStyle = false;
							__this.viewer._element.style.cursor = '';
							$('body').removeClass('drawCur').addClass('drawCur');
						}else{
							__this.viewer.enableCursorStyle = true;
							$('body').removeClass('drawCur');
						}
					});
					handlerPolygon && handlerPolygon.deactivate();
                    handlerPolygon && handlerPolygon.activate();
                });
                var IDs=[];
                function processCompleted(queryEventArgs){
                    var selectedFeatures = queryEventArgs.originResult.features;
                    var color;
                    IDs=[];
                    __this.viewer.entities.removeAll();
                    for(var i = 0; i < selectedFeatures.length; i++){
                        var value = selectedFeatures[i].fieldValues["0"];
                        var feature = selectedFeatures[i];
                        for(var j = 0; j < feature.fieldNames.length; j++){
                            var index = j.toString();
                            if(j === 0){
                                var des = '<table class="cesium-infoBox-defaultTable"><tbody>' + '<tr><th>' + selectedFeatures[i].fieldNames["0"] + '</th><td>' + selectedFeatures[i].fieldValues["0"] + '</td></tr>';
                            }else if(j === feature.fieldNames.length - 1){
                                des += '<tr><th>' + selectedFeatures[i].fieldNames[index] + '</th><td>' + selectedFeatures[i].fieldValues[index] + '</td></tr>' + "</tbody></table>";
                            }else{
                                des += '<tr><th>' + selectedFeatures[i].fieldNames[index] + '</th><td>' + selectedFeatures[i].fieldValues[index] + '</td></tr>';
                            }
                        }
                        __this.viewer.entities.add({
                            position : Cesium.Cartesian3.fromDegrees(parseFloat(selectedFeatures[i].fieldValues["12"]), parseFloat(selectedFeatures[i].fieldValues["13"]), parseFloat(selectedFeatures[i].fieldValues["16"])),
                            billboard :{
                                image : './images/location4.png',
                                width:30,
                                height:40,
                            },
                            name : selectedFeatures[i].fieldValues["11"],
                            description: des
                        });
                        IDs.push(parseInt(value)+11);
                    }
                    var buildingLayer = scene.layers.find("Building@CBD");
                    if(IDs.length>0){
                        // buildingLayer.setSelection(IDs);
                    }
                }
                function query(drawGeometryArgs){
                    var getFeaturesByGeometryParameters, getFeaturesByGeometryService;
                    getFeaturesByGeometryParameters = new SuperMap.REST.GetFeaturesByGeometryParameters({
                        datasetNames: ["二维数据:building"],
                        toIndex:-1,
                        spatialQueryMode:SuperMap.REST.SpatialQueryMode.CONTAIN,
                        geometry: drawGeometryArgs
                    });
                    var url = 'http://www.supermapol.com/realspace/services/data-cbd/rest/data';
                    getFeaturesByGeometryService = new SuperMap.REST.GetFeaturesByGeometryService(url, {
                        eventListeners: {
                            "processCompleted": processCompleted,
                            "processFailed": processFailed
                        }
                    });
                    getFeaturesByGeometryService.processAsync(getFeaturesByGeometryParameters);
                }
                function processFailed(e) {
                    alert(e.error.errorMsg);
                }
            }, function() {
                var title = '加载SCP失败,请检查网络连接状态或者url地址是否正确?';
                widget.showErrorPanel(title, undefined, e);
            });
        } catch (e) {
            if (widget._showRenderLoopErrors) {
                var title = '渲染时发生错误,已停止渲染。';
                widget.showErrorPanel(title, undefined, e);
            }
        }

        $("#loadingbar").remove();
    },
    methods:{
        convertPoint : function(point){
            var lonlatPoint = Cesium.Cartographic.fromCartesian(point);
            var x = Cesium.Math.toDegrees(lonlatPoint.longitude);
            var y = Cesium.Math.toDegrees(lonlatPoint.latitude);
            if(x && y){
                return new SuperMap.Geometry.Point(x, y);
            }
            return undefined;
        }
    }
};
</script>

<style>
.sm-compass {
    pointer-events: auto;
    position: absolute;
    right: 10px;
    top: 10px;
    width: 128px;
    height: 128px;
    overflow: hidden;
}
.sm-zoom {
	top: 130px;
}
.drawCur{
	cursor: url(/images/cur/draw.cur), auto;
}
.cesium-infoBox{
    top: 20;
    left: 0;
    transition: opacity 0.2s;
}
</style>