OpenLayers 4 - 半径为米的圆形功能不起作用?

时间:2017-10-11 05:17:23

标签: geometry meter

我现在从OpenLayers 2来到OpenLayers 4。 我想绘制一个特殊半径为米的圆圈。

我试过了:

var circleStyle = new ol.style.Style({
        fill: new ol.style.Fill({ color: 'rgba(255,0,0,0.4)' }),
        stroke: new ol.style.Stroke({ color: 'rgb(255,0,0)', width: 2 }),
});
//var units = map.getView().getProjection().getUnits();
// var radiusM = circle.getRadius() * ol.proj.METERS_PER_UNIT[units];

var circleFeature = new ol.Feature({
    geometry: new ol.geom.Circle(ol.proj.fromLonLat([Element.Position.Longitude, Element.Position.Latitude]), MetersToRadius(Element.RadiusInMetern)),
    UserID: Element.UserID,
    ID: Element.ID
});

circleFeature.setStyle(circleStyle);
vectorSource.addFeature(circleFeature); 

但是当我用control.ScaleLine检查圆圈时它很小!

我找到了这段代码,但它在OpenLayer 4上无效。

  

对象“ projection.getPointResolution (resolutionAtEquator,   中心);“不存在!

 var view = map.getView();
    var projection = view.getProjection();
    var resolutionAtEquator = view.getResolution();
    var center = map.getView().getCenter();
    var pointResolution=projection.getPointResolution(resolutionAtEquator, center);
    var resolutionFactor = resolutionAtEquator / pointResolution;
    var radius = (radius / ol.proj.METERS_PER_UNIT.m) * resolutionFactor;

    return(radius);

1 个答案:

答案 0 :(得分:2)

你需要使用

ol.proj.getPointResolution(projection,resolutionAtEquator,center);