如何在openlayers中绘制半径圆

时间:2014-04-24 09:18:03

标签: openlayers gwt-openlayers

我想在具有特定半径(km)的openlayers中画一个圆圈。具有特定地理点的中心?我跟http://demo.gwt-openlayers.org/gwt_ol_showcase/GwtOpenLayersShowcase.html?example=Draw%20Regular%20Polygon%20Example一样,但我想用预定义的值自动绘制。

2 个答案:

答案 0 :(得分:3)

以下代码将为您提供一个以当前地图中心为中心的浅蓝色圆圈,其半径以米为单位。如果你想要它的英尺,只需替换:

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

使用:

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

代码:

   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。此处创建的OpenLayers圈始终是一个完全平坦的2d圈。而实际上,你可能想要更好地考虑地图投影的东西(特别是靠近极点)。

答案 1 :(得分:0)

这样的东西
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);