代码之家  ›  专栏  ›  技术社区  ›  Captain Caveman

绘制形状时设置OpenLayers 3光标的样式

  •  0
  • Captain Caveman  · 技术社区  · 6 年前

    我有一个功能,允许用户在OpenLayers地图上绘制一个正方形或矩形。我想更改光标的样式。默认情况下,光标是一个蓝色的圆。我想把它改成一个正方形,这样的符号匹配的形状,用户可以创建。

    $scope.drawBoundingBox = () => {
        const bbVector = new ol.source.Vector({ wrapX: false });
        const vector = new ol.layer.Vector({
          source: bbVector
        });
        bbVector.on("addfeature", evt => {
          $scope.coords = evt.feature.getGeometry().getCoordinates();
        });
        const style = new ol.style.Style({
          stroke: new ol.style.Stroke({
            color: "#FFF",
            width: 3
          }),
          fill: new ol.style.Fill({
            color: [255, 255, 255, 0]
          })
        });
        const geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
        draw = new ol.interaction.Draw({
          source: bbVector,
          type: "Circle",
          geometryFunction
        });
        vector.set("name", "boundingBox");
        vector.setStyle(style);
        map.addLayer(vector);
        map.addInteraction(draw);
      };
    
    1 回复  |  直到 6 年前
        1
  •  0
  •   Captain Caveman    6 年前

    下面是一个可行的解决方案,它将默认的蓝色圆光标更改为正方形,并允许用户在地图上创建一个正方形或矩形形状。

      $scope.drawBoundingBox = () => {
        const bbVector = new ol.source.Vector({ wrapX: false });
        const vector = new ol.layer.Vector({
          source: bbVector
        });
        bbVector.on("addfeature", evt => {
          $scope.coords = evt.feature.getGeometry().getCoordinates();
        });
    
        const geometryFunction = ol.interaction.Draw.createRegularPolygon(4);
    
        draw = new ol.interaction.Draw({
          source: bbVector,
          type: "Circle",
          geometryFunction: geometryFunction,
          style: new ol.style.Style({
            stroke: new ol.style.Stroke({
              color: "#FFF",
              width: 3
            }),
            fill: new ol.style.Fill({
              color: [255, 255, 255, 0]
            }),
            geometryFunction,
            image: new ol.style.RegularShape({
              fill: new ol.style.Fill({
                color: '#FFF'
              }),
              stroke: new ol.style.Stroke({
                color: "blue",
                width: 3
              }),
              points: 4,
              radius: 10,
              angle: Math.PI / 4
            }),
          }),
        });
        vector.set("name", "boundingBox");
        map.addLayer(vector);
        map.addInteraction(draw);
      };