3

我现在从 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, center);” 不存在!

 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);
4

1 回答 1

3

你需要使用

ol.proj.getPointResolution(projection,resolutionAtEquator,center);
于 2018-03-23T20:06:00.177 回答