JAVA - Google maps api recover line distance

Asked

Viewed 474 times

0

I am developing an app for android that uses the google maps API, everything is right with the app the problem is in the webservice that deals with the information collected by the app, at a certain time I create a Polyline in the app and send the coordinates to the WS, I need to check in the WS what distance of any point of the line I created for example, suppose my line starts at the point -25.428781,-49.263291 and end at the point -25.431941,-49.261888, I need to know the distance from the point -25.431069,-49.263251 from my LINE, not the distance from the third point to the first and from the third point to the second, I need to know the distance from the LINE. On Android has a library that does this to Google’s own android-maps-utils, but I could not use it in my WS because it is a package .aar and I am using Maven, I accept suggestions for solutions directly to the problem as well as solutions to add this .aar in my project because both would solve the problem. To simplify I leave here the image of the map: Rotas

I need to know the distance of the GREEN line between the reference point and the route.

  • 1

    This is a matter of geometry. See Point-Line Distance-2-Dimensional and use equation 14.

  • Yes, I was already researching how to calculate point distance from the line :) the only problem is using latitudes and longitudes for the calculation, but as this feature is available on android I thought I have something similar available for a java application.

  • About the editing of tags I do not agree that this doubt is related to android because as I said, in the application is working normally, the problem is in WS that is java(spring boot). I don’t know how to communicate this disagreement so I posted as a comment.

  • I edited now. In situations that do not agree with the edition you can reverse the editing or edit the question and put the tags you want.

  • At first I thought that the answer in the ramaral would not help me, but I tested and worked perfectly. I had a little work to convert the result to meters by using latitudes and longitudes as a reference, but gave certinho, thanks.

  • Put an answer with your solution.

Show 1 more comment

1 answer

1

As recommended by @ramaral here’s the solution.

I used as reference the geographical positions (latitude and longitude)

I then have my straight that goes from -25.428781,-49.263291 until -25.431941,-49.261888 and I have the point that I want to know the distance that is the -25.431069,-49.263251 I then applied the formula to discover the distance of a point from the line (Equation 14 - Point-Line Distance-2-Dimensional ) in that way:

Double startLat = -25.428781;
Double startLng = -49.263291;

Double endLat = -25.431941;
Double endLng = -49.261888;

Double myLocationLat = -25.431069;
Double myLocationLng = -49.263251;

Double distancia = (((endLat - startLat) * (startLng - myLocationLng)) - ((startLat - myLocationLat) * (endLng - startLng))) / Math.sqrt( (Math.pow( (endLat - startLat), 2) + Math.pow( (endLng - startLng), 2) ) );

However, I achieved the distance in degrees (which is one of the units of measurement of latitudes and longitudes). To get the value in meters I had to research a little more and found how to convert from degrees to kilometers and then to meters that way:

( (distancia * 111.325) * 1000)

Each degree of latitude is equivalent to 111,325 kilometers, after multiplying the result of the equation by that value multiplied by 1000 to obtain the result in meters. In my tests the accuracy was approximately 2 meters, which in my case is enough.

Browser other questions tagged

You are not signed in. Login or sign up in order to post.