Google 地图 V3 几何库 - 插值不返回预期的纬度/经度
我花了几个小时研究谷歌地图几何库的插值函数的一个奇怪问题。 (请参阅:http://code.google.com/apis/地图/文档/javascript/reference.html#spherical) 我使用以下 javascript 代码来说明问题:
// be sure to include: https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false
// this works just as expected
var origin = new google.maps.LatLng(47.45732443, 8.570993570000041);
var destination = new google.maps.LatLng(47.45733, 8.570889999999963);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, destination);
console.log("origin:\r\nlat: " + origin.lat() + ", lng: " + origin.lng());
console.log("destination:\r\nlat: " + destination.lat() + ", lng: " + destination.lng());
console.log("distance between origin and destination: " + distance);
console.log("interpolating 50 equal segments between origin and destination");
for (i=1; i <= 50; i++) {
var step = (1/50);
var interpolated = google.maps.geometry.spherical.interpolate(origin, destination, step * i);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, interpolated);
console.log("lat: " + interpolated.lat() + ", lng: " + interpolated.lng() + ", dist: " + distance);
}
// the following does not work as expected
// the "interpolated" location is always equal to the origin
var origin = new google.maps.LatLng(47.45756, 8.572350000000029);
var destination = new google.maps.LatLng(47.45753, 8.57233999999994);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, destination);
console.log("origin:\r\nlat: " + origin.lat() + ", lng: " + origin.lng());
console.log("destination:\r\nlat: " + destination.lat() + ", lng: " + destination.lng());
console.log("distance between origin and destination: " + distance);
console.log("interpolating 50 equal segments between origin and destination");
for (i=1; i <= 50; i++) {
var step = (1/50);
var interpolated = google.maps.geometry.spherical.interpolate(origin, destination, step * i);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, interpolated);
console.log("lat: " + interpolated.lat() + ", lng: " + interpolated.lng() + ", dist: " + distance);
}
插值函数似乎不喜欢第二组纬度/经度对。它始终返回原点纬度/经度,而不是基于传递的分数 (1/50 * i) 的正确插值位置。
我尝试颠倒出发地和目的地,但结果是一样的。
任何关于我做错了什么的想法都非常感谢!
I have spent hours on a strange problem with the interpolate function of google maps' geometry library. (see: http://code.google.com/apis/maps/documentation/javascript/reference.html#spherical)
I use the following javascript code to illustrate the problem:
// be sure to include: https://maps.googleapis.com/maps/api/js?libraries=geometry&sensor=false
// this works just as expected
var origin = new google.maps.LatLng(47.45732443, 8.570993570000041);
var destination = new google.maps.LatLng(47.45733, 8.570889999999963);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, destination);
console.log("origin:\r\nlat: " + origin.lat() + ", lng: " + origin.lng());
console.log("destination:\r\nlat: " + destination.lat() + ", lng: " + destination.lng());
console.log("distance between origin and destination: " + distance);
console.log("interpolating 50 equal segments between origin and destination");
for (i=1; i <= 50; i++) {
var step = (1/50);
var interpolated = google.maps.geometry.spherical.interpolate(origin, destination, step * i);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, interpolated);
console.log("lat: " + interpolated.lat() + ", lng: " + interpolated.lng() + ", dist: " + distance);
}
// the following does not work as expected
// the "interpolated" location is always equal to the origin
var origin = new google.maps.LatLng(47.45756, 8.572350000000029);
var destination = new google.maps.LatLng(47.45753, 8.57233999999994);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, destination);
console.log("origin:\r\nlat: " + origin.lat() + ", lng: " + origin.lng());
console.log("destination:\r\nlat: " + destination.lat() + ", lng: " + destination.lng());
console.log("distance between origin and destination: " + distance);
console.log("interpolating 50 equal segments between origin and destination");
for (i=1; i <= 50; i++) {
var step = (1/50);
var interpolated = google.maps.geometry.spherical.interpolate(origin, destination, step * i);
var distance = google.maps.geometry.spherical.computeDistanceBetween(origin, interpolated);
console.log("lat: " + interpolated.lat() + ", lng: " + interpolated.lng() + ", dist: " + distance);
}
It appears that the interpolate function does NOT like the second set of lat/lng pairs. It always returns the origin lat/lng rather than the correctly interpolated location based on the fraction passed (1/50 * i).
I tried reversing origin and destination, but the outcome is the same.
Any ideas as to what I'm doing wrong are much appreciated!
如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。
绑定邮箱获取回复消息
由于您还没有绑定你的真实邮箱,如果其他用户或者作者回复了您的评论,将不能在第一时间通知您!
发布评论
评论(3)
我已提交有关此问题的问题:
https://issuetracker.google.com/issues/260343763
对我来说,问题是我正在使用这个函数来插入动画。我最终将该函数复制到我自己的源代码中,并删除了检查“r<1.0E-6”的 if 语句(正如 @davethebrave 所指出的)
I have filed an issue concerning this problem:
https://issuetracker.google.com/issues/260343763
For me the problem was that I am using this function to interpolate an animation. I ended up copying the function into my own source code and removing the if statement that checks for "r<1.0E-6" (as @davethebrave has pointed out)
事实证明,插值函数有一个内置限制,指定两点之间的距离必须大于 1.0E-6。
这对我来说仍然有点神秘,因为 1.0E-6 应该是 0.000001,而不是我测试中的 6.0。也许这是一个仅在使用 google.maps.gjsload 时才会出现的错误?我将进行更多测试并评论我的发现。
我通过简单地注释掉 if 语句来解决这个问题:
我希望这能帮助其他遇到同样问题的人。
As it turns out, the interpolate function has a built in limitation that specifies that the distance between the two points must be larger than 1.0E-6.
This is still somewhat a mystery to me, as 1.0E-6 should be 0.000001 and not 6.0 as it is in my tests. Perhaps this is a bug that only shows when using google.maps.gjsload? I'll test a bit more and comment on my findings.
I got around this by simply commenting out the if statement:
I hope this will help someone else out there running into the same problem.
我认为您对插值的准确性期望过高。纬度差异为
47.45756 - 47.45753 = 0.00003 度 ~ 3.3 米
。经度差异为8.57235- 8.57234 = 0.00001 deg ~ 0.5 米
(非常近似,请参见 维基百科)。现在,您将近似欧几里得距离3m
划分为 50 个间隔,查找距离 ca 的点。6 厘米
。将此与长度约为4,003,020,000 厘米
的地球赤道进行比较。I think you expect too much accuracy from the interpolation. The difference in the latitudes is
47.45756 - 47.45753 = 0.00003 deg ~ 3.3 meter
. The difference in the longitudes is8.57235- 8.57234 = 0.00001 deg ~ 0.5 meter
(very appoximatively, see Wikipedia). Now you divide the approximative Euclidean distance3m
into 50 intervals, looking for points at a distance of ca.6 cm
. Compare this with the Earth equator whose length is about4,003,020,000 cm
.