user472875
user472875

Reputation: 3175

Google Maps determine distance along Line

I am trying to determine the distance of a point along a given Polyline (from the start point) in Google maps (given that the user clicks on the Polyline and I get the point coordinates in the event).

So far, this is the only thing that comes to mind:

Sadly, this seems rather complicated for something that should be straightforward to do.

Is there any easier way?

P.S.: I'm using API v3

Upvotes: 2

Views: 1617

Answers (2)

user472875
user472875

Reputation: 3175

So, after much searching I decided to implement the algorithm as described above. Turned out it isn't as bad as I thought. Should anyone ever land on this page, the full code is below:

var DistanceFromStart = function (/*latlng*/ markerPosition) {

    var path = this.polyline.getPath();      
    var minValue = Infinity;
    var minIndex = 0;
    var x = markerPosition.lat();
    var y = markerPosition.lng();

    for (var i = 0; i < path.getLength() - 1; i++) {

        var x1 = path.getAt(i).lat();
        var y1 = path.getAt(i).lng();

        var x2 = path.getAt(i + 1).lat();
        var y2 = path.getAt(i + 1).lng();

        var dist = pDistance(x, y, x1, y1, x2, y2);

        if (dist < minValue) {
            minIndex = i;
            minValue = dist;
        }
    }      

    var gdist = google.maps.geometry.spherical.computeDistanceBetween;
    var dinit = gdist(markerPosition, path.getAt(minIndex));
    var dtotal = gdist(path.getAt(minIndex), path.getAt(minIndex + 1));

    var distanceFromStart = 0;

    for (var i = 0; i <= minIndex - 1; i++) {
        distanceFromStart += gdist(path.getAt(i), path.getAt(i + 1));
    }

    distanceFromStart += dtotal * dinit / dtotal;

    return distanceFromStart;
}

function pDistance(x, y, x1, y1, x2, y2) {

    var A = x - x1;
    var B = y - y1;
    var C = x2 - x1;
    var D = y2 - y1;

    var dot = A * C + B * D;
    var len_sq = C * C + D * D;
    var param = dot / len_sq;

    var xx, yy;

    if (param < 0 || (x1 == x2 && y1 == y2)) {
        xx = x1;
        yy = y1;
    }
    else if (param > 1) {
        xx = x2;
        yy = y2;
    }
    else {
        xx = x1 + param * C;
        yy = y1 + param * D;
    }

    var dx = x - xx;
    var dy = y - yy;
    return Math.sqrt(dx * dx + dy * dy);
}

If you see anything to improve, do let me know.

Upvotes: 3

DIXONJWDD
DIXONJWDD

Reputation: 1286

If you get the coordinates for the start and end points, then use the haversine algorithm to calculate the distance you can easily find the distance between two points taking into consideration the curvature of the earth.

Here is the formula (you may need to convert in into the language you are using):

var R = 6371; // km
var dLat = (lat2-lat1).toRad();
var dLon = (lon2-lon1).toRad();
var lat1 = lat1.toRad();
var lat2 = lat2.toRad();

var a = Math.sin(dLat/2) * Math.sin(dLat/2) +
    Math.sin(dLon/2) * Math.sin(dLon/2) * Math.cos(lat1) * Math.cos(lat2); 
var c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a)); 
var d = R * c;

variable d is your distance.

Hope this helps

Upvotes: -2

Related Questions