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.

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);
  }
  • 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
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.

Your Answer

By clicking “Post Your Answer”, you agree to our terms of service, privacy policy and cookie policy

Not the answer you're looking for? Browse other questions tagged or ask your own question.