I am developing an application using Google Maps. I have some logic which expects very consistent output form the google.maps.geometry.spherical
library.
Basically I am trying to draw a box with precise dimensions.
My sample code looks like this:
function (LatLng) {
var NW, NE, SW, SE, length, dist;
// LatLng is a coordinate
NW = LatLng
// I have a length variable
length = 25
// Using the single reference point as NorthWest corner of the box,
// I calculate the remaining 3 corners
NE = google.maps.geometry.spherical.computeOffset(LatLng, length, 90);
SW = google.maps.geometry.spherical.computeOffset(LatLng, length, 180);
SE = google.maps.geometry.spherical.computeOffset(SW, length, 90);
// now I check the dimensions
dist = google.maps.geometry.spherical.computeDistanceBetween(NE, NW);
console.log('NW/NE bounds distance', dist);
}
I am expecting to get a value of 25 (equal to length
), and indeed my subsequent logic depends on dist >= length
. However, when I run this multiple times I get some degree of variability around 25. I find anything from range 22.499999999193243
to 22.50000000221681
for example.
Is this variability to be expected? Is this a feature of the developer API?
If so, does anyone have suggestions on how to account for this in my code, as simply adding 0.000000001
to the dimensions seems a bit fudgey to me.
P.S.
This is a simplified version of my problem. In the real code I am actually generating a Rectangle
object, then calculating the distances between corners to compare to length
. However the principal, and error, is exactly the same.