简体   繁体   English

OpenLayers 4中如何按比例缩放?

[英]How zoom base on scale in OpenLayers 4?

I want to zoom to ol3 map base on scale(for example 1:50000). 我想基于比例缩放到ol3地图(例如1:50000)。 This means every 1cm in ol map must show 50000cm in real world. 这意味着ol地图中的每1cm必须在现实世界中显示50000cm。 for converting scale to resolution I used as follows: 用于将比例转换为分辨率的方法如下:

var getResolutionFromScale = function(scale, units) {
    var dpi = getDpi();
    var mpu = ol.proj.METERS_PER_UNIT[units];
    var inchesPerMeter = 39.37;
    return parseFloat(scale) / (mpu * inchesPerMeter * dpi);
}

function getDPI()
{
    var div = document.createElement("div");
    div.style.width="1in";
    var body = document.getElementsByTagName("body")[0];
    body.appendChild(div);
    var dpi = document.defaultView.getComputedStyle(div, null).getPropertyValue('width');
    body.removeChild(div);
    return parseFloat(dpi);
}

Now for testing it, I use ScaleLine and when scaleLine controle show 1000m, it's length must be 2cm, but it is almost 2.5cm. 现在进行测试,我使用ScaleLine,当scaleLine控件显示1000m时,其长度必须为2cm,但几乎为2.5cm。

Where is th problem? 问题在哪里? How can I zoom base on scale? 如何缩放比例?

Online Demo 在线演示

The projection is a Mercator projection that preserve angles but not the distance. 该投影是墨卡托投影 ,该投影保留角度但不保留距离。 This means that the scale is not the same on the whole map :( 这意味着在整个地图上比例尺是不一样的:(

To see it, just move from equator to the pole and see the ScaleLine length growing. 要看到它,只需从赤道移动到极点,然后看ScaleLine长度就会增加。

Thus you just can't calculate the scale worldwide, you have to calculate it locally (calculate a distance between 2 points at the center of the view in the map projection and the haversine distance to get the ratio). 因此,您无法在全球范围内计算比例,而必须在本地进行计算(计算地图投影中视图中心的2个点之间的距离与hasrsine距离以得出比例)。 Be aware the DPI depends on the device (screen, printer) your are using and may be quite different form one to another. 请注意,DPI取决于您使用的设备(屏幕,打印机),并且彼此之间可能有很大不同。

You can use the geometry.getLength() and ol.Sphere.getLength() to compute the 2 distances. 您可以使用geometry.getLength()ol.Sphere.getLength()来计算两个距离。 Look at the ol example: https://openlayers.org/en/latest/examples/measure.html 查看以下示例: https : //openlayers.org/en/latest/examples/measure.html

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM