Kousik
Kousik

Reputation: 22425

how to get the traveled distance from a group of geolocation points mapped in google map api?

I am traking user movement according to their latitude and longitude.so for last 1hr i got more than 20 coordinates.Using that co-ordinates i am drawing the map using google map api.I got one curly line(user's movement graph) and i want the get the distance from the staring point to ending point.

these are my co-ordinates:-

[[12.938419, 77.62224],
[12.938494, 77.622377],
[12.938369, 77.622449],
[12.938345, 77.622521],
[12.938322, 77.622575],
[12.938346, 77.622631],
[12.938306, 77.622648],
[12.938299, 77.622695],
[12.938254, 77.622715],
[12.938242, 77.622761],
[12.938227, 77.622805],
[12.93819, 77.622792],
[12.938138, 77.622837],
[12.938129, 77.622887],
[12.938103, 77.622949],
[12.938066, 77.622989],
[12.938006, 77.622966],
[12.937933, 77.623001],
[12.937976, 77.623073],
[12.937954, 77.623128],
[12.937912, 77.623111],
[12.937882, 77.623034],
[12.937933, 77.623001],
[12.938006, 77.622966],
[12.937921, 77.62293]]

Upvotes: 1

Views: 198

Answers (1)

geocodezip
geocodezip

Reputation: 161334

Get the distance between each set of points using google.maps.geometry.spherical.computeDistanceBetween (convert each point to a google.maps.LatLng first). Sum those distances for the total.

Or use the google.maps.geometry.spherical.computeLength method on an array of google.maps.LatLng objects created from your coordinates.

fiddle

for (var i=0; i < coordinates.length; i++) {
    path.push(new google.maps.LatLng(coordinates[i][0],
                                     coordinates[i][1]));
}
var polyline = new google.maps.Polyline({
    path: path,
    map: map,
    strokeWeight: 2,
    strokeColor: "blue"
});
var length = google.maps.geometry.spherical.computeLength(polyline.getPath());
document.getElementById('length').innerHTML = "length="+(length/1000).toFixed(2)+ " km";

Upvotes: 2

Related Questions