2

So I'm working in a project that needs a feature to identify when an object (in my case a car) is currently in front or behind another one using Google Maps. I currently have the bearing (cars orientation) and the distance between both cars.

So far my approach is that if car2 has the same bearing as car1 and the distance between both cars is reducing, car1 gets an alert that car2 is behind. But, this only work if I'm in car1. car2 will also receive a false alarm saying that car1 is also behind him, wich is not true (car1 is on front). This is because I don't know which car is getting close to the other one due to the fact that I only have distance between them.

So the question is: Is there a way to know when a location is getting close to another one with the GPS and GoogleMaps? Maybe comparing Lat and Lon between both?

Jose Bernhardt
  • 887
  • 1
  • 10
  • 24

1 Answers1

2

Calculate the bearing of the vector from one car (A) to another (B) (the code is here) and compare that to the bearing (direction of movement) of the "one car" A to calculate a relative direction.

relativeBearing = bearingToOtherCar - bearingOfThisCar;

If A is behind and B is in front then the relative direction from A to B is close to 0. In the opposite case it's close to 180. If B is on the left side it's 270 (or -90 if you like) and on the right side it's 90.

You just need handle the 359...0 transition properly when calculating the relative direction. If it's less than -180 then add 360 and if it's more than 180 then deduct it from 360:

if (relativeBearing < -180) relativeBearing = 360 + relativeBearing;
if (relativeBearing > 180) relativeBearing = 360 - relativeBearing;

This will give negative values for relative directions on the left side (-180...+180 scheme) and the values can be forced to the 0...359 scheme by adding 360 to any negative result.

Markus Kauppinen
  • 3,025
  • 4
  • 20
  • 30
  • 1
    Great answer , but I have a concern. I'm testing the left side relative bearing and the results are close to -90, but if then I calculate the absolute value of -90 my result will get mess up and it wont be true for the right side condition. So I guess I will always need to sum 360 to make close to 270. – Jose Bernhardt Nov 23 '16 at 17:31
  • I guess my mind was in a wrong place when writing about the absolute value, as I've previously calculated those for other purposes and ignored the direction. It of course doesn't make sense in this context. Left should be -90 unless you want to force the result to the 0...359 scheme (by adding 360 to negative values) and follow the scheme that Android uses for the GPS bearings. It's of course up to you to decide should left be -90 or 270. – Markus Kauppinen Nov 23 '16 at 18:31
  • The idea of modifying the values < -180 and > 180 is of course to pick the smaller section of the full circle, as if the result were e.g. -200 the minus sign would refer to the left side of the circle, but in reality that direction is at rear-right at 160. – Markus Kauppinen Nov 23 '16 at 18:31
  • Okay, now seems legit, thanks so much for the help. – Jose Bernhardt Nov 23 '16 at 18:33