获取 OpenLayers 中 2 个 LonLat 对象之间的距离(以像素为单位)
我有 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 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(2)
我发现了问题:destinationVincenty() 需要并返回 wgs84 中的坐标,其中我的地图使用球面墨卡托投影。
我希望我得到了正确的答案,因为投影让我头晕,从来没有真正理解它们:(。我在控制台中查找我的坐标的数字以及用于计算 getViewPortPxFromLonLat 的 map.getExtent() 的坐标(),我意识到它们的数量级不正确,然后我就明白了,
所以,代码现在是:
用 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:
Measured the circles with the OpenLayers.Control.ScaleLine, and the size is dead on :D
看来你做得对。如果你得到的距离太小,可能是
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