获取 OpenLayers 中 2 个 LonLat 对象之间的距离(以像素为单位)

发布于 2024-11-18 17:39:15 字数 698 浏览 1 评论 0原文

我有 2 个 OpenLayers.LonLat 对象,我想确定这两个对象之间当前缩放的像素距离。我使用 OpenLayers.Layer.getViewPortPxFromLonLat() 来确定点的 x 和 y,然后减去以查看两者之间的差异,但对于相距 2000 公里的点,我得到的值非常小。

这是我的代码:

        var center_lonlat = new OpenLayers.LonLat(geometry.lon, geometry.lat);
        var center_px = layer.getViewPortPxFromLonLat(center_lonlat);

        var radius_m = parseFloat(feature.attributes["radius"]);
        var radius_lonlat = OpenLayers.Util.destinationVincenty(center_lonlat, 0, radius_m);
        var radius_px = layer.getViewPortPxFromLonLat(radius_lonlat);

        var radius = radius_px.y - center_px.y;

我试图在这里画一个圆,因为我收到了一个中心点和以米为单位的半径。 LonLat 对象似乎没问题。

我做错了什么吗?

I have 2 OpenLayers.LonLat objects, and I want to determine the distance in pixels for the current zoom between the 2. I'm using OpenLayers.Layer.getViewPortPxFromLonLat() to determine the x and y of the points and then subtract to see the difference between the 2, but the values that I get are very small for points that are 2000km apart.

Here is my code:

        var center_lonlat = new OpenLayers.LonLat(geometry.lon, geometry.lat);
        var center_px = layer.getViewPortPxFromLonLat(center_lonlat);

        var radius_m = parseFloat(feature.attributes["radius"]);
        var radius_lonlat = OpenLayers.Util.destinationVincenty(center_lonlat, 0, radius_m);
        var radius_px = layer.getViewPortPxFromLonLat(radius_lonlat);

        var radius = radius_px.y - center_px.y;

I'm trying here to draw a circle, giving that I receive a center point and a radius in meters. The LonLat object seems to be ok.

Am I doing something wrong ?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

与他有关 2024-11-25 17:39:15

我发现了问题:destinationVincenty() 需要并返回 wgs84 中的坐标,其中我的地图使用球面墨卡托投影。

我希望我得到了正确的答案,因为投影让我头晕,从来没有真正理解它们:(。我在控制台中查找我的坐标的数字以及用于计算 getViewPortPxFromLonLat 的 map.getExtent() 的坐标(),我意识到它们的数量级不正确,然后我就明白了,

所以,代码现在是:

        var spherical_mercator = new OpenLayers.Projection("EPSG:900913");
        var wgs84 = new OpenLayers.Projection("EPSG:4326");

        var map = feature.layer.map;
        var geometry = feature.geometry;
        var center_lonlat = new OpenLayers.LonLat(geometry.y, geometry.x);
        var center_px = map.getViewPortPxFromLonLat(center_lonlat);

        var radius_m = parseFloat(feature.attributes["radius"]);
        var radius_lonlat = OpenLayers.Util.destinationVincenty(center_lonlat.clone().transform(spherical_mercator, wgs84), 0, radius_m).transform(wgs84, spherical_mercator);
        var radius_px = map.getViewPortPxFromLonLat(radius_lonlat);

        var radius = Math.abs(radius_px.y - center_px.y);

用 OpenLayers.Control.ScaleLine 测量圆圈,尺寸是准确的。 :D

I found the issue: destinationVincenty() need and returns coordinates in wgs84 where my map was using spherical mercator projection.

I hope I got correctly the answer, because projections make me dizzy and never really understood them :(. I was looking in the console to the numbers for my coordinates and the coordinates from the map.getExtent() that is used to calculate the getViewPortPxFromLonLat() and I realised they are not in the right order of magnitude, and then it hit me.

So, the code is now:

        var spherical_mercator = new OpenLayers.Projection("EPSG:900913");
        var wgs84 = new OpenLayers.Projection("EPSG:4326");

        var map = feature.layer.map;
        var geometry = feature.geometry;
        var center_lonlat = new OpenLayers.LonLat(geometry.y, geometry.x);
        var center_px = map.getViewPortPxFromLonLat(center_lonlat);

        var radius_m = parseFloat(feature.attributes["radius"]);
        var radius_lonlat = OpenLayers.Util.destinationVincenty(center_lonlat.clone().transform(spherical_mercator, wgs84), 0, radius_m).transform(wgs84, spherical_mercator);
        var radius_px = map.getViewPortPxFromLonLat(radius_lonlat);

        var radius = Math.abs(radius_px.y - center_px.y);

Measured the circles with the OpenLayers.Control.ScaleLine, and the size is dead on :D

颜漓半夏 2024-11-25 17:39:15

看来你做得对。如果你得到的距离太小,可能是OpenLayers.Util.destinationVincenty函数有问题?您是否尝试过用其他东西更换轴承(0) - 它的价值在您的情况下似乎并不重要。但坦率地说,我在浏览 来源

You seem to be doing right. If the distance you get is too small, maybe there is a problem with OpenLayers.Util.destinationVincenty function? Have you tried to replace the bearing (0) with anything else - its value seem to be not important in your case. But frankly speaking, I wasn't able to understand how it works while browsing the source

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文