openlayers学习二

openlayers学习二

实现地图标记路线
在这里插入图片描述

地图上的点位在openlayers中都抽象为特征点。翻阅文档说明

ol/Feature~Feature

import Feature from 'ol/Feature';

A vector object for geographic features with a geometry and other attribute properties, similar to the features in vector file formats like GeoJSON.

Features can be styled individually with setStyle; otherwise they use the style of their vector layer.

Note that attribute properties are set as module:ol/Object~BaseObject properties on the feature object, so they are observable, and have get/set accessors.

Typically, a feature has a single geometry property. You can set the geometry using the setGeometry method and get it with getGeometry. It is possible to store more than one geometry on a feature using attribute properties. By default, the geometry used for rendering is identified by the property name geometry. If you want to use another geometry property for rendering, use the setGeometryName method to change the attribute property associated with the geometry for the feature. For example:

import Feature from 'ol/Feature';
import Polygon from 'ol/geom/Polygon';
import Point from 'ol/geom/Point';

var feature = new Feature({
  geometry: new Polygon(polyCoords),
  labelPoint: new Point(labelCoords),
  name: 'My Polygon'
});

// get the polygon geometry
var poly = feature.getGeometry();

// Render the feature as a point using the coordinates from labelPoint
feature.setGeometryName('labelPoint');

// get the point geometry
var point = feature.getGeometry();

Feature可以用来描述点、线。接受的参数是point和、lineString

     let endLocation = new Feature({
        geometry: new Point([record.range[2], record.range[3]])
      });
    
    let routerLine = new Feature({
        geometry: new LineString(lineArry)
      });

可以使用前端WKT的readGeometry()方法解析空间几何对象转化为Poin和LineString对象所需要的二维点数组、数组

    let format = new WKT();
    let lineArry = format.readGeometry(record.geom).getCoordinates();

线路,点位设计样式。

源码中提示,这里设计性能问题。把创建的样式存储在数组中,在使用的时候直接挂载到图层上即可,减少for循环大量创建特征点导致的卡顿现象,减少浏览器内存开销

    let  clickStyles =  {
      startStyle : new Style({
        image: new Icon({
          src: new URL(`../assets/start.svg`, import.meta.url).href,
          anchor: [0.5, 1],
        })
      }),
      endStyle : new Style({
        image: new Icon({
          src: new URL(`../assets/end.svg`, import.meta.url).href,
          anchor: [0.5, 1],
        })
      }),
      routeClickStyle : new Style({
        stroke: new Stroke({
          width: 2, //线的宽度
          color: "#ffc641", //线的颜色
        }),
      }),
      // 1 = 已完成
      routeNotClickStyle : new Style({
        stroke: new Stroke({
          width: 2, //线的宽度
          color: "#7af55d", //线的颜色
        }),
      }),
      // 0 = 进行中
      routeNotClickStatusIngStyle : new Style({
        stroke: new Stroke({
          width: 2, //线的宽度
          color: "#1958ee", //线的颜色
        }),
      })
    }

特征点挂载样式

startLocation.setStyle(clickStyles.startStyle);
endLocation.setStyle(clickStyles.endStyle);
routerLine.setStyle(clickStyles.routeClickStyle);

图像层挂载路线图层,连续在layerIcon添加3组特征点即可

      layerIcon.getSource().addFeature(startLocation);
      layerIcon.getSource().addFeature(endLocation);
      layerIcon.getSource().addFeature(routerLine);

最终挂载到图像层上

let layerIcon = new VectorLayer({
  source: new VectorSource(),
})
layerIcon.getSource().addFeature(routerLine);
  • 0
    点赞
  • 2
    收藏
    觉得还不错? 一键收藏
  • 1
    评论

“相关推荐”对你有帮助么?

  • 非常没帮助
  • 没帮助
  • 一般
  • 有帮助
  • 非常有帮助
提交
评论 1
添加红包

请填写红包祝福语或标题

红包个数最小为10个

红包金额最低5元

当前余额3.43前往充值 >
需支付:10.00
成就一亿技术人!
领取后你会自动成为博主和红包主的粉丝 规则
hope_wisdom
发出的红包
实付
使用余额支付
点击重新获取
扫码支付
钱包余额 0

抵扣说明:

1.余额是钱包充值的虚拟货币,按照1:1的比例进行支付金额的抵扣。
2.余额无法直接购买下载,可以购买VIP、付费专栏及课程。

余额充值