use of ol.gwt.CollectionWrapper in project gwt-ol3 by TDesjardins.
the class OLUtil method getZoomLevel.
/**
* Gets the current zoomlevel of the given {@link Map}.
* @param map
* {@link Map}
* @return zoomlevel on success, else {@link Double#NaN}
*/
public static double getZoomLevel(Map map) {
View v = map.getView();
// try to get zoom
double z = getZoom(v);
if (!Double.isNaN(z)) {
return z;
}
// zoom is undefined, so check resolution
double zoomResolution = v.getResolution();
// walk layers to find resolution
CollectionWrapper<Base> layers = new CollectionWrapper<Base>(map.getLayers());
for (Base l : layers) {
// get source if layer instance has it
Source source = l.get("source");
if (source != null) {
// try to get a tilegrid from the source
TileGrid tg = getTileGrid(source);
if (tg != null) {
// check resolutions
double[] resolutions = tg.getResolutions();
if (resolutions != null) {
double dPreviousResolution = 0;
for (int i = 0; i < resolutions.length; i++) {
// resolutions are sorted in descending order, so
// compare with actual one
double resolution = resolutions[i];
if (resolution <= zoomResolution) {
if (i > 1) {
// calculate the delta of the resolution
// compared to the current and the previous
// zoomlevel
double delta = (resolution - zoomResolution) / (dPreviousResolution - resolution);
// adjust the integer zoomlevel to the delta
return i + delta;
} else {
return 0;
}
}
dPreviousResolution = resolution;
}
}
}
}
}
return Double.NaN;
}
Aggregations