簡體   English   中英

為什么在 Openlayers 中創建多邊形時需要 getPointResolution

[英]Why do i need getPointResolution when creating polygons in Openlayers

所以我看到了一個創建多邊形圓來縮放的例子。 這是從興趣點到半徑的實際距離。

  map.on('postrender', function (event) {
        const { projection, resolution, center, } = event.frameState.viewState;
        pointResolution = getPointResolution(projection.getCode(), resolution, center);
    });

是否有必要這樣做var newradius = radius / pointResolution因為這並沒有給我以米為單位的正確距離。 它實際上將它分開。 我不知道這是否有意義。

 function addCirclePolygon(coordinates) {
        if (vectorSource && vectorSource.getFeatures().length > 0) {
            vectorSource.clear();
        }
        var radius = $scope.options.radius;
        if (createPolygon) {
            **var radius = radius / pointResolution;** <---problem
            circle = new Feature(new Circle(coordinates, radius));
            circle.setStyle(new Style({
                stroke: new Stroke({
                    color: $scope.options.lcolor,
                    width: $scope.options.lwidth
                }),
                fill: new Fill({
                    color: $scope.options.fcolor ? $scope.options.fcolor : 'rgba(255, 255, 255, 0.0)'
                })
            }));
            var geom = circle.get('geometry');
            if (circle instanceof Circle) {
                // We don't like circles, at all.  So here we convert
                // drawn circles to their equivalent polygons.  
                circle.set('geometry', fromCircle(geom));
            }
            vectorSource.addFeature(circle);
            /**
             * Adds a line to show the radius in Units(meters as default)
             * Leaving this here for prosterity
             */
            if (circle) {
                let line = addRadius(circle);
                vectorSource.addFeature(line);
            }
        }
    }

tldr; 您不需要 getPointResolution 來創建圓形多邊形。

您可以使用 geom.Polygon.circular 創建具有正確半徑的藍色圓圈。

new ol.geom.Polygon.circular([lng, lat], radius);

如果您計划創建一個圓(綠色)或創建一個 polygon.fromCircle(紅色),則需要將半徑除以分辨率。

https://codepen.io/dbauszus-glx/pen/LYmjZvP

在此處輸入圖像描述

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM