首页 > 其他分享 >openlayers 在地图上绘制矩形框,非鼠标框选

openlayers 在地图上绘制矩形框,非鼠标框选

时间:2023-02-23 09:57:44浏览次数:68  
标签:supermap layer ol 矩形框 source openlayers new var 框选


<!DOCTYPE html>
<html>
<head>
<meta charset="UTF-8" />
<title data-i18n="resources.title_tiledMapLayer3857"></title>
<script type="text/javascript" src="../js/include-web.js"></script>
<script type="text/javascript" src="../../dist/ol/include-ol.js"></script>
</head>
<body style="margin: 0; overflow: hidden; background: #fff; width: 100%; height: 100%; position: absolute; top: 0">
<div id="map" style="width: 100%; height: 100%"></div>
<script type="text/javascript">
      var map,
        url =
          (window.isLocal ? window.server : 'https://iserver.supermap.io') +
          '/iserver/services/map-china400/rest/maps/China';
      // 方式一:1.调用 ol.supermap.initMap,根据 SuperMap iServer 地图服务的地图信息,创建地图和底图
    //   ol.supermap.initMap(url, {
    //     mapOptions: {
    //       controls: ol.control
    //         .defaults({ attributionOptions: { collapsed: false } })
    //         .extend([new ol.supermap.control.Logo()])
    //     }
    //   });
      
      /* 方式二:1.调用 ol.supermap.MapService,获取 SuperMap iServer 地图服务的地图信息
                2.调用 ol.supermap.viewOptionsFromMapJSON 获取地图视图参数
                3.调用 ol.Map 创建地图
                4.调用 ol.layer.Tile 与 ol.source.TileSuperMapRest 创建底图
      */
      new ol.supermap.MapService(url).getMapInfo(function (serviceResult) {
        const mapObj = serviceResult.result;
        map = new ol.Map({
          target: 'map',
          controls: ol.control
            .defaults({ attributionOptions: { collapsed: false } })
            .extend([new ol.supermap.control.Logo()]),
          view: new ol.View(ol.supermap.viewOptionsFromMapJSON(mapObj))
        });
        var layer = new ol.layer.Tile({
          source: new ol.source.TileSuperMapRest(ol.source.TileSuperMapRest.optionsFromMapJSON(url, mapObj, true))
        });
        map.addLayer(layer);
        map.addControl(new ol.supermap.control.ScaleLine());
        
        
        var coords = [[110,20],[112,22],[114,25],[116,28],[111,40],[110,20]]; // 矩形坐标数组
        
        var vectorLayer = new ol.layer.Vector();
        var vectorSource = new ol.source.Vector();
        var polygon = new ol.geom.Polygon([coords]); // 矩形构造方法,不知道这里为啥又套了一层数组,去掉这层数组则无法显示矩形
        polygon.applyTransform(ol.proj.getTransform('EPSG:4326', 'EPSG:3857')); // 4326 对应的是经纬度坐标系,3857叫墨卡托?反正不是经纬度,需要做个转换
        vectorSource.addFeature(new ol.Feature(polygon)); // 形成Feature,并添加到source
        vectorLayer.setSource(vectorSource); // layer设置source
        map.addLayer(vectorLayer); // 将layer加入地图
      });
    </script>
</body>
</html>

谁知道这里是为什么又套了一层数组?
var polygon = new ol.geom.Polygon([coords]); // 矩形构造方法,不知道这里为啥又套了一层数组,去掉这层数组则无法显示矩形
如果这么写的话,按照上面的例子,传入的内容为[ [ [110,20],[112,22],[114,25],[116,28],[111,40],[110,20] ] ]岂不是没有按照一般人的思路来,为什么多套一层?

reference:

  1. SuperMap REST地图服务底图 3857
  2. OpenLayers API - Class: Polygon

标签:supermap,layer,ol,矩形框,source,openlayers,new,var,框选
From: https://www.cnblogs.com/echo-lovely/p/17146843.html

相关文章

  • Openlayers 通过canvas渲染部分地图
    效果图实现原理简单描述Openlayers图层的渲染大多数都是通过canvas实现,在图层渲染前后事件中通过canvas控制渲染区域即可实现。代码点击查看代码componentDidMoun......
  • UI画框选人加动态烘焙导航移动
     usingSystem.Collections;usingSystem.Collections.Generic;usingUnityEngine;usingUnityEngine.AI;publicclassInputMag:MonoBehaviour{privateboolis......
  • [openlayers07]——加载天地图为底图并展示GeoJSON
    [openlayers07]_加载天地图为底图并展示GeoJSON1.加载底图(得申请key)天地图2.创建地图、设置center和投影//创建地图constcenter=[114.1692,30.494];//EPSG:4......
  • [openlayers06]——根据features的属性设置style
    [openlayers06]_根据features的属性设置style1.说明Makingitlooknice我们会希望不同的要素展示成不用的样式(style),下面的内容:展示如何根据features自身的属性设......
  • [openlayers05]_——Downloading_features(下载功能)
    [openlayers05]_Downloadingfeatures(下载功能)1.说明Downloadingfeatures在用户上传数据并编辑之后,我们希望用户能下载结果。为此,我们将featuredata转为GeoJSON......
  • [openlayers04]——Drawing new features
    [openlayers04]_Drawingnewfeatures1.效果展示gif展示(有可能不展示):绘制结果2.完整代码js代码import'./style.css';importMapfrom'ol/Map.js';import......
  • [openlayers03]——Modifying features
    Modifyingfeatures1.说明功能:让用户能编辑数据,使用ModifyModifyingfeatures结果展示2.步骤导入ModifyimportModifyfrom'ol/interaction/Modify';......
  • openlayers01——使用openlayers展示GeoJSON数据
    使用openlayers展示GeoJSON数据文中包含geojson数据获取、选取坐标、并将坐标作为center和展示geojson1.获取数据1.1从阿里云获取数据阿里云数据可视化平台获取......
  • openlayers--点位、边界样式设置
    //创建中心点文字样式createPointStyle(feature){returnnewol.style.Style({text:newol.style.Text({//位置textAlign:'center',//基准线......
  • openlayers--添加数据源
    调用地图服务接口后,对接口返回值做处理--添加数据源if(res.features.length){//获取--根据行政区代码查询对应行政区的中心点letlons=res.features[0].properties.L......