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!
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 is 8.57235- 8.57234 = 0.00001 deg ~ 0.5 meter
(very appoximatively, see Wikipedia). Now you divide the approximative Euclidean distance 3m
into 50 intervals, looking for points at a distance of ca. 6 cm
. Compare this with the Earth equator whose length is about 4,003,020,000 cm
.