Take the 2-minute tour ×
Geographic Information Systems Stack Exchange is a question and answer site for cartographers, geographers and GIS professionals. It's 100% free, no registration required.

Hi i am using distanceTo from OpenLayers to calculate the distance between two locations

these are map details

    Geographic  = new OpenLayers.Projection("EPSG:4326"); 
    Mercator = new OpenLayers.Projection("EPSG:900913");


   map = new OpenLayers.Map({
   div: "map_canvas",
   projection: Mercator,
   displayProjection: Geographic,
   center: new OpenLayers.LonLat(0, 0),
   minResolution: "auto",
   minExtent: new OpenLayers.Bounds(-1, -1, 1, 1),
   maxResolution: "auto",
   maxExtent: new OpenLayers.Bounds(-180, -90, 180, 90),
       units : 'km'

}); these are my lat and lon values

lat = 47.162494, lon = 19.503304
lat = 46.2525684, lon = 20.147060499999952

google maps function result 111861.5962110549

function distanceBetweenMarkers(latlng1, latlng2){
    return google.maps.geometry.spherical.computeDistanceBetween(latlng1, latlng2);
}

openlayers function result 6237980.800397318

function distanceBetweenMarkers(latlng1, latlng2){
        var point1 = new OpenLayers.Geometry.Point(latlng1.lon, latlng1.lat);
        var point2 = new OpenLayers.Geometry.Point(latlng2.lon, latlng2.lat);
        return point1.distanceTo(point2);
    }

why is the difference between google maps computeDistanceBetween and openlayers distanceTo?

share|improve this question
2  
I wonder where your (lon,lat) coordinates come from: look at how far apart the longitudes are! If they are correct, then these points must be almost a third of the way around the globe from each other (within Szeged and Indianapolis, I believe). If the earth were only 107 miles in diameter, then that distance would indeed be 100 miles. The earth hasn't shrunk that much yet :-). Because its diameter really is 12,706,000 meters, the actual distance is about 11,807,000 meters--not too far from your calculation, which is not bad considering you are using the high-distortion Mercator projection. –  whuber May 17 '12 at 21:01

1 Answer 1

up vote 2 down vote accepted

try to use getGeodesicLength.

it works for calculating the approximate length of the geometry were it projected onto the earth.

distanceTo method works for planar measure which handle the world as a flat using cartesian coordinates. getting approximately the accurate results , you should use geodesic measurament for your project.

var point1 = new OpenLayers.Geometry.Point(latlng1.lon, latlng1.lat);
var point2 = new OpenLayers.Geometry.Point(latlng2.lon, latlng2.lat);

var line = new OpenLayers.Geometry.LineString([p1, p2]);
alert(line.getGeodesicLength(new OpenLayers.Projection("EPSG:4326")));

i hope it helps you...

share|improve this answer
    
i want to replicate the google maps google.maps.geometry.spherical.computeDistanceBetween(latlng1, latlng2) –  Ramesh Kotha May 18 '12 at 16:02
    
Ramesh, if the Google maps result truly is 100 miles as you still claim in your edited question, then there's something seriously wrong that needs to be fixed first. –  whuber May 18 '12 at 17:16
    
hi @whuber, can u suggest me what i have to use in openlayers to get the same result with google maps above function. –  Ramesh Kotha May 19 '12 at 10:16
    
What result does Google Maps give, Ramesh? I don't believe it would tell you those cities are 100 miles apart! –  whuber May 19 '12 at 14:21
    
hi @whuber i have updated the code. please look into it.. –  Ramesh Kotha May 19 '12 at 21:02

Your Answer

 
discard

By posting your answer, you agree to the privacy policy and terms of service.

Not the answer you're looking for? Browse other questions tagged or ask your own question.