Search code examples
javascriptopenlayersgeodesic-sphere

How to implement a geodesic option for the Scale Bar add-in in OpenLayers 2?


I am using the Scale Bar Add-in from OpenLayers 2 and it doesn't have a geodesic option.

I know that the Scale Line has a geodesic option, but considering that the Scale Bar has a wider variety of options, it would be nice to introduce he geodesic option in this one too.

Could anyone help me with this? How could I introduce a geodesic option in the Scale Bar Add-in?

Thank you


Solution

  • I think I solved it.

    First of all, I added a geodesic option, so the user can choose if he wantss the geodesic measures or not.

    Second, I created this function (based on the ScaleLine):

    getGeodesicRatio: function() {
        var res = this.map.getResolution();
        if (!res) {
            return;
        }
    
        var curMapUnits = this.map.getUnits();
        var inches = OpenLayers.INCHES_PER_UNIT;
    
        // convert maxWidth to map units
        var maxSizeData = this.maxWidth * res * inches[curMapUnits];
        var geodesicRatio = 1;
        if(this.geodesic) {
            var maxSizeGeodesic = (this.map.getGeodesicPixelSize().w ||
                0.000001) * this.maxWidth;
            var maxSizeKilometers = maxSizeData / inches["km"];
            geodesicRatio = maxSizeGeodesic / maxSizeKilometers;
        }
        return geodesicRatio;
    },
    

    Third, I inserted the geodesic ratio in other functions:

    "getComp"
    var ppdu = OpenLayers.DOTS_PER_INCH * system.inches[unitIndex]
        / this.getGeodesicRatio() / this.scale;
    
    "setSubPros"
    var ppdu = OpenLayers.DOTS_PER_INCH * system.inches[unitIndex]
        / this.getGeodesicRatio() / this.scale;
    

    I hope this is useful.