[OpenLayers-Users] Distance errors on Google maps

Peter Robins openlayers at peterrobins.co.uk
Sun Feb 1 11:29:45 EST 2009


I have a program that displays kml linestrings on either topo maps or google. 
I'm using getLength() to display the length of the segments, and noticed 
significant errors on the google display, in some cases 60% or more. I'm 
also using the scaleline, which is also wrong on Google.

Thinking this was probably some error in my logic, I tried some simple tests. 
The topo maps conveniently include a 1km grid, so I drew some 1km lines on 
the grid (as close as I could make it) and saved them to kml. This gave me 
coordinates that I knew were 1km apart (on the topomap projection). I then 
used the Google api:

var p1 = new GLatLng(42.87420777239693,-8.547039046722437);
var p2 = new GLatLng(42.87483907990915,-8.534868375601192);
alert(p1.distanceFrom(p2));

This gave me 0.995km, so Google agrees that these 2 points are 1km apart. 
(Google's docs say their calculations may be up to 0.3% out because of the 
spherical assumption they use; this is no problem at the scales I'm using.)

I then took OL's spherical mercator example page, and added my kml file. The 
scale line incorrectly showed the 1km line as well over 1km long. I then did:

var p1 = new OpenLayers.Geometry.Point(-8.534868375601192,42.87483907990915);
p1.transform(map.displayProjection, map.projection);
var p2 = new OpenLayers.Geometry.Point(-8.547039046722437,42.87420777239693);
p2.transform(map.displayProjection, map.projection);
alert(p1.distanceTo(p2));

This gave me 1.358km.
Thinking there might be some problem with the spherical mercator calculations, 
I tried removing 'sphericalMercator: true' and using proj with different 
projection objects. I found several defs on spatialreference.org, 
and used SR-ORG:6 and SR-ORG:6627; I also used the new EPSG:3785. These defs 
are all very similar, so I was not surprised to get the same error: 1358m.

Those 2 points are in Spain, EPSG:23030, but I also tried other places. One 
5.2km line in GB, as measured on OS maps, EPSG:27700, was 8.4km according to 
OL Google getLength().

I would expect discrepancies when dealing with different projections. I would 
expect rounding errors when transforming lots of points on a linestring. But 
these errors are so large the figures are effectively useless.

Am I doing something wrong, or missing some vital step somewhere? Is it an 
error in OL's calculations? Or is it one of those complicated projection 
issues, where it would be simpler to use Google's api not OL if I want 
linestring lengths?



More information about the Users mailing list