JAVA - Google maps api recover distance of the line

0

I'm developing an app for android that uses google maps API, it's all right with the app the problem is in the webservice that handles the information gathered by the app, at some point I create a Polyline in the app and send the coordinates to WS, I need to check in WS how far from any point on the line I've created for example, suppose my line starts at point -25.428781,-49.263291 and ends at point -25.431941,-49.261888 , I need to know the distance from point -25.431069,-49.263251 of my LINE, not the distance from the third point to the first and nor from the third point to the second, I need to know the distance from the LINE. In Android it has a library that does this to Google's own android-maps-utils, however I could not use it in my WS because it is a .aar package and I'm using maven, I accept suggestions for solutions directly to the problem as well as workarounds for adding this .aar to my project as both would solve the problem. For simplicity I leave here the image of the map:

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

    
asked by anonymous 08.04.2017 / 16:09

1 answer

1

As recommended by the @ramaral here is the solution.

I used geographical positions (latitude and longitude)

I have then my line that goes from -25.428781,-49.263291 to -25.431941,-49.261888 and I have the point that I want to know the distance that is -25.431069,-49.263251 Then I applied the formula to find the distance from a point on the line (Equation 14 - Point-Line Distance - 2-Dimensional ) in this 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 got 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 search a bit more and I found how to convert from degrees to kilometers and then to meters like this:

( (distancia * 111.325) * 1000)

Each degree of latitude equals 111.325 km, after multiplying the result of the equation by this 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.

    
10.04.2017 / 02:42