JavaScript-Google Maps V3几何库-插值不返回预期的纬度/经度
作者:互联网
我花了几个小时来解决Google地图几何库的插值函数的奇怪问题. (请参阅: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);
}
似乎插值函数不喜欢第二组经纬度对.它始终返回原始纬度/经度,而不是基于传递的分数(1/50 * i)正确插补的位置.
我尝试反转起点和终点,但结果相同.
关于我在做什么错的任何想法都非常感谢!
解决方法:
我认为您期望插值的准确性过多.纬度差为47.45756-47.45753 = 0.00003度〜3.3米.经度的差是8.57235- 8.57234 = 0.00001度〜0.5米(非常近似,请参见Wikipedia).现在,将欧几里得近似距离3m分为50个间隔,寻找距离为ca的点. 6厘米与此相比,地球赤道的长度约为4,003,020,000厘米.
标签:google-maps-api-3,geometry,javascript,interpolation 来源: https://codeday.me/bug/20191102/1989198.html