3

I want to draw a circle in openlayers with specific radius in km. with the center of specific geo point? I follow same as http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Draw%20Regular%20Polygon%20Example but I want to draw automatically with predefined values.

Mayank Pandya
  • 1,593
  • 2
  • 17
  • 40

2 Answers2

7

The following code will give you a circle in light blue centered at the center of the current map with a radius defined in meters. If you want it in feet, just replace:

        var radius = (radius / ol.proj.METERS_PER_UNIT.m) * resolutionFactor;

with:

        var radius = (radius / ol.proj.METERS_PER_UNIT.ft) * resolutionFactor;

Code:

   var drawCircleInMeter = function(map, radius) {
        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;


        var circle = new ol.geom.Circle(center, radius);
        var circleFeature = new ol.Feature(circle);

        // Source and vector layer
        var vectorSource = new ol.source.Vector({
            projection: 'EPSG:4326'
        });
        vectorSource.addFeature(circleFeature);
        var vectorLayer = new ol.layer.Vector({
            source: vectorSource
        });

        map.addLayer(vectorLayer);
    }

NB. The OpenLayers circle created here is always a perfectly flat 2d circle. Whereas in reality you may want something that takes better account of the map projection (especially close to the poles).

Peter Becker
  • 8,795
  • 7
  • 41
  • 64
Alexandre Mélard
  • 12,229
  • 3
  • 36
  • 46
  • 1
    I get the error: TypeError: projection.getPointResolution is not a function. – myhappycoding Sep 14 '20 at 16:19
  • 1
    I had the same issue with the new version of openlayer it's: `var pointResolution = getPointResolution(projection, resolutionAtEquator, center);` with `import { getPointResolution } from 'ol/proj';` – dekajoo Oct 31 '20 at 09:02
0

Something like

Style stopPointStyle = new Style();
stopPointStyle.setPointRadius(15);
stopPointStyle.setFillColor("red");
stopPointStyle.setStrokeColor("blue");
stopPointStyle.setStrokeWidth(1);
stopPointStyle.setFillOpacity(0.5);

Point point = new Point(journalEntry.getLongitude(), journalEntry.getLatitude());
VectorFeature pointFeature = new VectorFeature(point, stopPointStyle);
Knarf
  • 2,077
  • 1
  • 16
  • 24
  • Thanks for your answer but I want radius in KM – Mayank Pandya Apr 24 '14 at 11:23
  • 1
    There are methods in Openlayers to do calculations between px and lonlat. And there are libs to do calculations between lonlat and km. In this example some stuff is done with converting pixels and lonlat. Doing the correct calculations is just some java and shouldn't be to hard: http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Image%20vector – Knarf Apr 24 '14 at 12:10
  • What projection are you using? I am assuming you are working in something projected, as you said meters, but if not, conversion is always possible. You can draw a circle programmatically in pure openlayers (I know nothing about gwt) by explicitly creating a points array and then creating a OpenLayers.Geometry.LinearRing from that and an OpenLayers.Feature.Vector from the LinearRing. You would have to use basic trigonometry to generate the circle points around some center point with a given radius. I can put up an example in jsFiddle, but want to be sure I'm answering the right question. – John Powell Apr 24 '14 at 21:54
  • The answer John gives should also work in the GWT version. The projection question is indeed relevant. Cause in some projections a circle with a radius of 20Km isn't projected as a circle but as an oval. (note that I am a developer of gwt-openlayers) – Knarf Apr 25 '14 at 06:40
  • @Knarf, good to know. It is indeed true about the ovals, though of course it depends on latitude and circle radius, as well as the projection, as you say. – John Powell Apr 25 '14 at 07:46