views:

53

answers:

4

I'm trying to find a function lng = f(lat) that would help me draw a line between 2 given GPS coordinates, (lat1, lng1) and (lat2, lng2).

I've tried the traditional Cartesian formula y=mx+b where m=(y2-y1)/(x2-x1), but GPS coordinates don't seem to behave that way.

What would be a formula/algorithm that could help me achieve my goal.

PS: I'm using Google Maps API but let's keep this implementation agnostic if possible.

UPDATE: My implementation was wrong and it seems the algorithm is actually working as stated by some of the answers. My bad :(

schema

+2  A: 

What you want to do should actually work. Keep in mind however that if north is on top, the horizontal (x) axis is the LONGITUDE and the vertical (y) axis is the LATITUDE (I think you might have confused this).

If you parametrize the line as lat = func(long) you will run into trouble with vertical lines (i.e. those going exactly north south) as the latitude varies while the longitude is fixed.

Therefore I'd rather use another parametrization:

long(alpha) = long_1 + alpha * (long_2 - long_1)

lat(alpha)  = lat_1  + alpha * (lat_2  - lat_1)

and vary alpha from 0 to 1.

This will not exactly coincide with a great circle (shortest path on a sphere) but the smaller the region you are looking at, the less noticeable the difference will be (as others posters here pointed out).

Andre Holzner
A: 

Here is a distance formula I use that may help. This is using javascript.

function Distance(lat1, lat2, lon1, lon2) {
        var R = 6371; // km
        var dLat = toRad(lat2 - lat1);
        var dLon = toRad(lon2 - lon1);
        var a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) * Math.sin(dLon / 2) * Math.sin(dLon / 2);

        var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));

        var d = R * c * 0.621371;

        var r = Math.round(d * 100) / 100;
        return r;
    }
Caimen
@Caimen, this is the "haversine formula" for distance over the curvature of the earth between two points. How could that help him draw a line?
LarsH
A: 

For short distances, where the earth curvature doesn't make a significant difference, it works fine to draw a line using regular two-dimensional geometry.

For longer distances the shortest way between two lines does not project as a straight line on a map, but as a curve. (For example, the shortest way from Sweden to Alaska would be straight over the noth pole, not past Canada and Iceland.) You would have to use three-dimensional geometry to draw a line on a surface of a sphere, then project that onto the map in the same way the earth surface is projected on the map.

Guffa
A: 

Is your goal to find this equation or to actually draw a line?

If the latter, since you're using the Maps API, specify geodesic: true and draw it with a Polyline:

http://code.google.com/apis/maps/documentation/javascript/reference.html#Polyline

broady
Thanks but I was actually trying to animate a marker.
Olivier Lalonde