1

What is the best method for getting the radius of a circle in Openlayers 3 in epsg 4326 (geodesic)? I'm using this example for a base and trying to draw a circle while getting the radius, but using proj 4326 is proving difficult. Here's my code:

var pointerMoveHandler = function(evt) {
   if (evt.dragging) {
     return;
  }
var tooltipCoord = evt.coordinate;

  if (sketch) {
    var output;
    var geom = (sketch.getGeometry());
    if (geom instanceof ol.geom.Cirlce) {
      output = formatCircle(/** @type {ol.geom.Circle} */ (geom));
      tooltipCoord = geom.getCenter().getRadius();

    measureTooltipElement.innerHTML = output;
    measureTooltip.setPosition(tooltipCoord);
  }
};

var sourceProj = map.getView().getProjection();
    var geom = /** @type {ol.geom.Circle} */(Circle.clone().transform(
        sourceProj, 'EPSG:4326'));
    var center= geom.getRadius;
    radius = Math.abs(wgs84Sphere.geodesicArea(center));
var output;
  if (area > 10000) {
    output = (Math.round(area / 1000000 * 100) / 100) +
        ' ' + 'km<sup>2</sup>';
  } else {
    output = (Math.round(area * 100) / 100) +
        ' ' + 'm<sup>2</sup>';
  }
  return output;
};

radius = Math.abs(wgs84Sphere.geodesicArea(center)); isn't right, but I'm not sure what to put there....

2
  • I need to know where do you get the function formatRadius(). Did you written by your own? Or did you found it somewhere on OL3 examples? Thank you
    – zorta86
    Mar 15, 2017 at 10:41
  • 1
    I received some help. It wasn't on the examples.
    – Dean
    Mar 16, 2017 at 12:40

1 Answer 1

2

Found it..

var formatRadius = function(Circle) { 
    var radius; 
        if (geodesicCheckbox.checked) { 
            var center = Circle.getCenter();
                var pointOnPerimeter = [center[0], center[1] + Circle.getRadius()]
            var sourceProj = map.getView().getProjection(); 
                var c1 = ol.proj.transform(center, sourceProj, 'EPSG:4326'); 
                var c2 = ol.proj.transform(pointOnPerimeter, sourceProj,                    'EPSG:4326'); 
                radius = wgs84Sphere.haversineDistance(c1, c2); 
        } else { 
             radius = Math.round(Circle.getRadius() * 100) / 100; 
        } 
        var output; 
         if (radius > 100) { 
            output = (Math.round(radius / 1000 * 100) / 100) + ' ' + 'km'; 
        } else { 
                output = (Math.round(radius * 100) / 100) + ' ' + 'm'; 
        } 
    return output; 
};

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.