Skip to content

Instantly share code, notes, and snippets.

@ca0v
Created May 10, 2018 14:56
Show Gist options
  • Save ca0v/0bc895b4f528b05e1abd7ab441317b7d to your computer and use it in GitHub Desktop.
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)
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 = "";
}
}
}
@ca0v
Copy link
Author

ca0v commented May 10, 2018

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

Sign up for free to join this conversation on GitHub. Already have an account? Sign in to comment