I'm using Mapbox for Android Java and need to calculate the distance between a drawn line defined by start and endpoint to the users current location.
Any idea on how to implement that with mapbox?
Asked
Active
Viewed 464 times
2

patpatwithhat
- 335
- 2
- 11
-
1I don't know about map box but the way to calculate the distance between a point and a line is to write the line equation as ax + by +c = D. If x,y is on the line the D will be 0. If it isn't it will be your distance. To get the equation of the line you need to rewrite the equation in the form ax+by+c=0 and you can get started. – AminM May 10 '22 at 21:31
-
Thanks, I already know that. I hoped for some included functions, making my life easier. – patpatwithhat May 11 '22 at 07:03
1 Answers
1
You have some Turf.js (a geospatial analysis library) methods ported to Android: https://docs.mapbox.com/android/java/guides/turf/
- Use
TurfMisc.nearestPointOnLine()
with the line and the user location as an input - Then use
TurfMeasurement.distance()
between the two points

Etherlind
- 311
- 2
- 8
-
Oh, thanks a lot! I looked for nearestPointOnLine in TurfMeasurement, where I didn't see it. This makes life way easier! – patpatwithhat May 14 '22 at 12:13