Bytechanger
Bytechanger

Reputation: 31

OpenLayers 4 - feature with circle with radius in meters doesn´t work?

I came from OpenLayers 2 now to OpenLayers 4. I want to draw a circle with a special radius of meters.

I tried:

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

but when I check the circle with control.ScaleLine it´s to small!

I found this code, but it doesn´t work on OpenLayer 4.

The object "projection.getPointResolution(resolutionAtEquator, center);" doesnt exists!

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

Upvotes: 3

Views: 1432

Answers (1)

Tom
Tom

Reputation: 31

you need to use

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

Upvotes: 3

Related Questions