Bug:比例尺(ScaleLine)在Openlayers v4.6.5中计算错误


该bug,官方已经在后续版本修正,故只针对OL 4.6+。在此自做记录。

场景:

1、引用Openlayers v4.6.5的js文件
2、view的坐标系采用EPSG:4326
3、比例尺的单位为米(metric)

原因分析:

在源码scaleline.js中,

......
ol.control.ScaleLine.prototype.updateElement_ = function() {
  ......

  var center = viewState.center;
  var projection = viewState.projection;
  var units = this.getUnits();
  var pointResolutionUnits = units == ol.control.ScaleLineUnits.DEGREES ?
    ol.proj.Units.DEGREES :
    ol.proj.Units.METERS;
  var pontResolution =
      ol.proj.getPointResolution(projection, viewState.resolution, center, pointResolutionUnits);
  if (units != ol.control.ScaleLineUnits.DEGREES) {
    pointResolution *= projection.getMetersPerUnit();
  }

......
};

ol.control.ScaleLine.Property_ = {
  UNITS: 'units'
};

问题就出在这里:

//step1:
var pontResolution =
      ol.proj.getPointResolution(projection, viewState.resolution, center, pointResolutionUnits);
//step2:
if (units != ol.control.ScaleLineUnits.DEGREES) {
    pointResolution *= projection.getMetersPerUnit();
 }

当比例尺单位不为度(degrees)时,会乘以 projection.getMetersPerUnit()的值,因此在视图为EPSG:4326等经纬度坐标系的情况下,pointResolution的计算错误。

解决:

1、官方IssuesOL 5.0.0 – ScaleLine slightly incorrect for EPSG:4326 with non-degree display units
2、更换版本
3、借鉴其他版本,修改本版本源码
4、针对实际应用系统,针对性地修改

转载自:https://blog.csdn.net/u013240519/article/details/83620442

You may also like...