It is possible to calculate the speed of the mobile device moving through the geolocation of Google Maps javascript for android ?
1 Answers
At least if you use the native geolocation service provided by Geolocation plugin you get the location accurate enough from which you can calculate the speed as
function calculateSpeed(t1, lat1, lng1, t2, lat2, lng2) {
// From Caspar Kleijne's answer starts
/** Converts numeric degrees to radians */
if (typeof(Number.prototype.toRad) === "undefined") {
Number.prototype.toRad = function() {
return this * Math.PI / 180;
}
}
// From Caspar Kleijne's answer ends
// From cletus' answer starts
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 distance = R * c;
// From cletus' answer ends
return distance / t2 - t1;
}
function firstGeolocationSuccess(position1) {
var t1 = Date.now();
navigator.geolocation.getCurrentPosition(
function (position2) {
var speed = calculateSpeed(t1 / 1000, position1.coords.latitude, position1.coords.longitude, Date.now() / 1000, position2.coords.latitude, position2.coords.longitude);
}
}
navigator.geolocation.getCurrentPosition(firstGeolocationSuccess);
where the toRad function for the Number is from Caspar Kleijne's answer, the calculation for distance between the two coordinates is from cletus' answer, the t2 and t1 are in seconds and the latitudes (lat1 & lat2) and longitudes (lng1 & lng2) are floats.
The main idea in code is the following: 1. Get initial location and store time when in that location, 2. Fetch another location, and when gotten, use the locations and times to call the calculateSpeed function.
The same formula of course applies to the Google Maps case, but in that case I'd review the exactness of calculation since even the network lag might cause some measurement errors that multiply easily if the time interval is too short.

- 1
- 1

- 7,326
- 4
- 43
- 66
-
So exactly , the code you posted , what exactly does ? Only calculate the speed , or the distance ? – none nane Jul 16 '15 at 14:19
-
Please read the cletus' answer about how to calculate the distance between two locations (expressed as coordinates). After we know the distance _d_, we get the speed (or usually called also velocity) _v_ with formula _v = d/(t2 - t1)_ where the _t2_ and _t1_ are the points in time when the measurements of locations were taken. When we do _t2 - t1_, we get the time used to travel the distance which again is the definition of velocity. For more information on how to calculate speed see [Wikipedia](https://en.wikipedia.org/wiki/Speed). – Roope Hakulinen Jul 16 '15 at 14:37
-
1I now have my coordinates thanks to geolocation cordova plugin , but now how do I pass them in scritp of ' cletus ' ? . I have ' latitude ' ' longitude ' , I have no variables . – none nane Jul 16 '15 at 15:25
-
@nonenane: Added more concrete example.Didn't test it though. You should be able to see the idea there. – Roope Hakulinen Jul 16 '15 at 15:42
-
I do not understand how to put my coordinates in the script you have posted . And another thing , how do I find the second point from which then calculate the distance ? – none nane Jul 16 '15 at 16:31
-
The complete example I provided calculates the speed for you if you just paste it in your application as it is. As you can see, it does the locating two times. For your actual use case you should of course modify it accordingly. – Roope Hakulinen Jul 16 '15 at 16:34
-
I'm no expert , then , what must change in the script that you put it? And how I display on the device the result of the variable speed ? – none nane Jul 17 '15 at 07:01
-
@nonenane: That isn't in the scope of this question really. For example if you want to just show the speed as alert you can add `alert(speed);` after line starting `var speed = `. If you wish to put it in your DOM, you could do something like `document.body.innerHTML += 'Your speed is ' + speed + '';` at the same spot on the code. – Roope Hakulinen Jul 17 '15 at 07:32
-
but in the first part of the code , such as the distance of the points is calculated ? The variables lat1 and Lat2, Dlat and Dlon are affixed so ? – none nane Jul 17 '15 at 14:21
-
I'm not quite sure what you are asking, but the naming of dLat and dLon comes from the [Greek character delta](https://en.wikipedia.org/wiki/Delta_%28letter%29) which stands for difference in physics in general. So in this case the dLat and dLon are differences in coordinates as [radians](https://en.wikipedia.org/wiki/Radian). – Roope Hakulinen Jul 17 '15 at 14:24
-
Thanks for all the answers , do you explain very well. There is one thing I do not understand , coordinates lat1 / 2 and Lon1 / 2 , I must not initialize them with my address ? And the second point that I would come loaded a few seconds after the first , now I do not think there is any timer – none nane Jul 17 '15 at 14:40
-
Yes, you are correct, there is no timer to add any additional delay for getting the location again. This way the two points (locations) are fetched one after another with something like few milliseconds in between of them. This is enough though if there is some movement all the time. If you wish you can use [`setTimeout`](http://www.w3schools.com/jsref/met_win_settimeout.asp) to wrap the `navigator.geolocation.getCurrentPosition` inside the `firstGeolocationSuccess`. – Roope Hakulinen Jul 17 '15 at 14:46
-
Plaese , you can add more comment in the script because I can not understand how it works and where it takes my coordinates to calculate the distance. Thanks – none nane Jul 17 '15 at 15:03