【发布时间】:2011-11-29 23:53:12
【问题描述】:
我花了几个小时来解决谷歌地图几何库的插值函数的一个奇怪问题。 (见:http://code.google.com/apis/maps/documentation/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);
}
似乎 interpolate 函数不喜欢第二组 lat/lng 对。它总是返回原点 lat/lng,而不是根据传递的分数 (1/50 * i) 正确插值的位置。
我试过颠倒起点和终点,但结果是一样的。
非常感谢任何关于我做错了什么的想法!
【问题讨论】:
标签: javascript google-maps-api-3 geometry interpolation