Created
May 10, 2018 14:56
-
-
Save ca0v/0bc895b4f528b05e1abd7ab441317b7d to your computer and use it in GitHub Desktop.
ol.control.Resolution: reports the resolution as number of meters in 96 pixels (unsure how to get the actual dpi)
This file contains hidden or bidirectional Unicode text that may be interpreted or compiled differently than what appears below. To review, open the file in an editor that reveals hidden Unicode characters.
Learn more about bidirectional Unicode characters
class ResolutionWatcher extends ol.control.Control { | |
private element: HTMLElement; | |
constructor(options?: olx.control.ControlOptions & { | |
className?: string; | |
render?: Function; | |
}) { | |
options = _.defaults(options || {}, { | |
element: document.createElement("div"), | |
className: "ol-resolution", | |
render: (mapEvent: ol.MapEvent) => this.renderResolution(mapEvent), | |
}); | |
super(options); | |
this.element = options.element as HTMLElement; | |
this.element.classList.add(options.className); | |
} | |
private renderResolution(mapEvent: ol.MapEvent) { | |
let frameState = mapEvent.frameState; | |
if (frameState) { | |
let viewState = frameState.viewState; | |
let projection = viewState.projection; | |
let resolution = viewState.resolution; | |
let metersPerUnit = projection.getMetersPerUnit(); | |
let metersPerPixel = resolution * metersPerUnit; | |
let pixelsPerInch = 96; | |
// 1 inch on the screen that is | |
let metersPerInch = metersPerPixel * pixelsPerInch; | |
this.element.innerHTML = `${metersPerInch.toPrecision(5)} m`; | |
this.element.title = `1 inch = ${metersPerInch.toPrecision(5)} meters`; | |
} else { | |
this.element.innerHTML = ""; | |
} | |
} | |
} |
Sign up for free
to join this conversation on GitHub.
Already have an account?
Sign in to comment
.ol-resolution {
position: absolute;
bottom: 0.5em;
right: 0.5em;
width: 1in;
border-left: 1px solid black;
border-right: 1px solid black;
text-align: center;
}