views:

52

answers:

1

Hi,

I have some problems with a great circle distance calculation using a map.

Context: http://airports.palzkill.de/search/

The map is supposed to work as a great circle distance search map - you move the circles center marker or the radius marker, and the circle gets smaller or larger. For debug purposes, the boxes title field shows the calculated distance in km.

This only works fine as long as the circle center is close to 0/0, and the radius marker is not too far away from it. The more you move either of the markers to "extremes", the more off some tangent the whole thing goes and produces nothing but crap.

This is the code used for calculating the updates, you can also find the entire code in the JS file js.js, lines 146 to 184:

function searchmapupdate()
{   
rad_lat_radiuspos = (circleradiusmarker.getPosition().lat()*Math.PI/180);
rad_lon_radiuspos = (circleradiusmarker.getPosition().lng()*Math.PI/180);
rad_lat_circlecenter = (circlecentermarker.getPosition().lat()*Math.PI/180);
rad_lon_circlecenter = (circlecentermarker.getPosition().lng()*Math.PI/180);

circleradiusvar = Math.acos(Math.sin(rad_lat_circlecenter)*Math.sin(rad_lat_radiuspos)+Math.cos(rad_lat_circlecenter)*Math.cos(rad_lon_radiuspos)*Math.cos(rad_lon_circlecenter-rad_lon_radiuspos))*6371.01*1000;

if (isNaN(circleradiusvar)==false) circle.setOptions({center:circlecentermarker.getPosition(), radius:circleradiusvar});

document.getElementById("mapsearchhead").innerHTML = Math.round(circleradiusvar/1000);
}

Since the whole thing does calculate some correct output I assume the math itself is not totally wrong, I guess there is just some "correctional" stuff missing? Unfortunately I am absolutely lousy at trigonometry, so I dont have a clue what might be wrong here, or even where to start searching for ideas about how to fix it.

Marco

P.S.: I know that due to the spherical nature of the projection, the whole thing has to act "counter-intuitive" around the poles. But that doesnt really explain what happens when you move both markers close to the date line around the equator (0/179, 0/-179).

+1  A: 

Okay, after implementing some more code, I actually discovered an error in the equation: Using lon where I should have used lat.

And yes, I am very embarassed now - and equally annoyed, having spent 5 hours looking at the terms and not finding this obvious error earlier.

Marco P.
We've all been there. Thanks for posting your fix.
GreenMatt