Vue.js2+Cesium1.103.0 十三、通过经纬度查询 GeoServer 发布的 wms 服务下的 feature 对象的相关信息

Vue.js2+Cesium1.103.0 十三、通过经纬度查询 GeoServer 发布的 wms 服务下的 feature 对象的相关信息

Demo

vue 复制代码
<template>
  <div
    id="cesium-container"
    style="width: 100%; height: 100%;"
  >
    <div style="position: absolute;z-index: 999;bottom: 0;left: 0;background: #fff;width: 100%;padding: 20px;box-sizing: border-box;">
      <div> {{ position }}</div>
      <div>{{ info }}</div>
    </div>
  </div>
</template>

  <script>
/* eslint-disable no-undef */
import axios from 'axios'
export default {
  data() {
    return {
      position: '',
      info: ''
    }
  },
  computed: {},
  watch: {},
  mounted() {
    const _this = this
    window.$InitMap()

    const imageryLayer = new Cesium.ImageryLayer(
      new Cesium.WebMapServiceImageryProvider({
        url: 'http://openlayers.vip/geoserver/cite/wms',
        layers: 'cite:2000',
        parameters: {
          transparent: true,
          format: 'image/png',
          srs: 'EPSG:4326'
        },
        tileWidth: 1024,
        tileHeight: 1024
      })
    )
    viewer.imageryLayers.add(imageryLayer)

    viewer.camera.flyTo({
      destination: Cesium.Rectangle.fromDegrees(
        114.4491417723215,
        38.96451275547338,
        118.24157311104125,
        41.29160446951736
      )
    })

    /**
     * @description: 根据用户点击的坐标计算 bbox 参数
     * @param {*} latlng
     * @param {*} zoom
     * @return {*}
     */
    function PositionToBbox(latlng, zoom) {
      const box = getZoomBbox(zoom)
      const boxMin = {
        lat: latlng.lat - box,
        lng: latlng.lng - box
      }
      const boxMax = {
        lat: latlng.lat + box,
        lng: latlng.lng + box
      }
      return `${boxMin.lng},${boxMin.lat},${boxMax.lng},${boxMax.lat}`
    }

    /**
     * @description: 计算用户坐标`应该减去的差值
     * @param {*} zoom
     * @return {*}
     */
    function getZoomBbox(zoom) {
      const level0 = 142.03125
      let box = level0 / Math.pow(2, zoom)
      box = box / 2
      return box
    }

    function geoServerQuery(data) {
      return axios({
        method: 'get',
        url: `http://openlayers.vip/geoserver/cite/wms`,
        headers: {
          // Authorization: "",
        },
        params: data
      })
        .then(res => {
          if (res && res.data) {
            return res.data
          }
        })
        .catch(() => {
          return false
        })
    }

    // 鼠标事件
    const handler = new Cesium.ScreenSpaceEventHandler(viewer.scene.canvas)
    handler.setInputAction(function (movement) {
      const earthPosition = viewer.camera.pickEllipsoid(
        movement.endPosition,
        viewer.scene.globe.ellipsoid
      )
      const cartographic = Cesium.Cartographic.fromCartesian(
        earthPosition,
        viewer.scene.globe.ellipsoid,
        new Cesium.Cartographic()
      )
      const longitude = Cesium.Math.toDegrees(cartographic.longitude)
      const latitude = Cesium.Math.toDegrees(cartographic.latitude)
      if (longitude && latitude) {
        console.log('.................', longitude, latitude)
        _this.position = `${longitude}, ${latitude}`
        geoServerQuery({
          service: 'wms',
          version: '1.1.1',
          request: 'getfeatureinfo',
          format: 'image/png',
          transparent: true,
          query_layers: 'cite:2000',
          layers: 'cite:2000',
          exceptions: 'application/vnd.ogc.se_inimage',
          info_format: 'application/json',
          feature_count: 50,
          x: 50,
          y: 50,
          srs: 'epsg:4490',
          width: 101,
          height: 101,
          bbox: PositionToBbox(
            {
              lng: longitude,
              lat: latitude
            },
            24
          )
        })
          .then(res => {
            console.log('..............res', res)
            if (res && res.features && res.features.length > 0) {
              _this.info = res.features[0].properties
            } else {
              _this.info = ''
            }
          })
          .catch(err => {
            _this.info = ''
            console.log(err)
          })
      }
    }, Cesium.ScreenSpaceEventType.MOUSE_MOVE)
  },
  methods: {}
}
</script>

<style>
</style>
相关推荐
2501_9209317011 小时前
React Native鸿蒙跨平台采用ScrollView的horizontal属性实现横向滚动实现特色游戏轮播和分类导航
javascript·react native·react.js·游戏·ecmascript·harmonyos
0思必得012 小时前
[Web自动化] Selenium处理动态网页
前端·爬虫·python·selenium·自动化
东东51613 小时前
智能社区管理系统的设计与实现ssm+vue
前端·javascript·vue.js·毕业设计·毕设
catino13 小时前
图片、文件的预览
前端·javascript
2501_9209317014 小时前
React Native鸿蒙跨平台实现推箱子游戏,完成玩家移动与箱子推动,当所有箱子都被推到目标位置时,玩家获胜
javascript·react native·react.js·游戏·ecmascript·harmonyos
layman052815 小时前
webpack5 css-loader:从基础到原理
前端·css·webpack
半桔15 小时前
【前端小站】CSS 样式美学:从基础语法到界面精筑的实战宝典
前端·css·html
AI老李15 小时前
PostCSS完全指南:功能/配置/插件/SourceMap/AST/插件开发/自定义语法
前端·javascript·postcss
_OP_CHEN15 小时前
【前端开发之CSS】(一)初识 CSS:网页化妆术的终极指南,新手也能轻松拿捏页面美化!
前端·css·html·网页开发·样式表·界面美化
啊哈一半醒15 小时前
CSS 主流布局
前端·css·css布局·标准流 浮动 定位·flex grid 响应式布局