5

I am new with OpenLayers 3. I'm working with the projection EPSG:4326.

I can get current resolution from view.getResolution(), but I am having trouble to get current scale of map. Please, give me a solution as soon as possible.

Gabor Farkas
  • 4,598
  • 2
  • 24
  • 37
Milan
  • 59
  • 1
  • 1
  • 2

2 Answers2

5

Below is a function to get the resolution from scale, you should be able to transform the other way round using the same logic:

  getResolutionForScale(scale, units) {
    var dpi = 25.4 / 0.28;
    var mpu = ol.proj.METERS_PER_UNIT[units];
    var inchesPerMeter = 39.37;
    return parseFloat(scale) / (mpu * inchesPerMeter * dpi);
  }
bartvde
  • 2,469
  • 10
  • 6
  • 1
    You assume that the DPI is 25.4 / 0.28 ... Why? Wouldn't the DPI change when you use the GIS app on a mobile device, for example? I am sorry if my questions sound silly, but I am still not very used to DPI calculations. – joaorodr84 Aug 30 '16 at 18:39
  • Where are the magic numbers (25.4, 0.28) coming from? :-) It would be nice to know! Thanks! – Benedek Simo Feb 11 '20 at 19:26
  • 1
    0.28 is defined in the OGC WMS specification as mm per pixel. 25.4 is just to get convert from inch to mm – bartvde Feb 13 '20 at 07:34
3

I think scale is not that useful for screen devices, you can obtain the scale by getting the resolution and multiplying it for the amount of pixels in your desired unit. The first answer to this question, Getting the physical screen dimensions / dpi / pixel density in Chrome on Android , explains with great detail how reference pixels correspond to real units, so 96 pixels should be equal to an inch and your scale would be 1:(resolution*96). I think a better approach is to draw a scale line as most sites do, and OpenLayers 3 has ol.control.ScaleLine for that.