简体   繁体   English

动态改变圆圈大小 openlayers

[英]Dynamically change circle size openlayers

I am trying to dynamically increase the circle fence size using :我正在尝试使用以下方法动态增加圆形围栏的大小:

$(document).on("keypress", "#radius", function () {
    circleFeature.set("radius",parseInt($("#radius").val()));
});

I initially created the circle using this code:我最初使用以下代码创建了圆圈:

 var circleFeature = new ol.Feature(circle);
    circleFeature.set('fenceId', fenceId);
    circleFeature.set('latitude', latitude);
    circleFeature.set('longitude', longitude);
    circleFeature.set('radius', givenRadius);
    circleFeature.set('circleRadius', radius);
    circleFeature.set('desc', desc);
    circleFeature.set('isActive', isActive);

When I press type the radius, the keypress event is triggered but the circle shape does not change in size.当我按下输入半径时,会触发按键事件,但圆形的大小不会改变。

You are changing your feature properties but not the properties of your ol.geom.geometry or ol.style.Style .您正在更改您的要素属性,而不是ol.geom.geometryol.style.Style的属性。 I suppose your geometry is ol.geom.Circle .我想你的几何是ol.geom.Circle

Then you can use this instead:然后你可以改用这个:

$(document).on("keypress", "#radius", function () {
    circleFeature.getGeometry().setRadius(parseInt($("#radius").val()));
});

While this PR is not merged use a style function like:虽然此 PR未合并,但使用如下样式函数:

var styleFunction = function(feature) {
  return [
    new ol.style.Style({
      image: new ol.style.Circle({
        radius: feature.get('radius'),
        fill: new ol.style.Fill({
          color: 'green'
        })
      })
    })
  ];
};

Then:然后:

$(document).on("keypress", "#radius", function () {
  circleFeature.set("radius",parseInt($("#radius").val()));
  circleFeature.setStyle(styleFunction);
  circleFeature.changed();
});

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM