-2

Im working on an Android app which is similar to Waze or Google Maps. I already have a polyline populated by Direction API

Now I need to calculate distance from Marker A to Marker B based on polyline generated

enter image description here

Im assuming that I can calculate the distance by looping the polyline coordinates, but the marker is in the middle of the polyline coordinate. As you can see the distance at the bottom, that is the straight line distance between marker A and marker B which is incorrect.

Does anyone have any idea how can I calculate the distance between Marker A to Marker B based on the polyline generated?

Any help would be great! Thanks! Please ask if you have any question

Alooza
  • 69
  • 12
  • 1
    Does this answer your question? [Find distance between two points on map using Google Map API V2](https://stackoverflow.com/questions/14394366/find-distance-between-two-points-on-map-using-google-map-api-v2) – Andrii Omelchenko Oct 06 '20 at 10:24
  • @AndriiOmelchenko you referring to SphericalUtil.computeDistanceBetween() function? No. I'm not looking to get a straight line distance between point A to point B. Like I mention in my question, I would like a distance between point A to point B based on the direction/polyline – Alooza Oct 07 '20 at 07:07

1 Answers1

1

Get the index of the marker A coordinate and also the index of the marker B coordinate from the list of coordinates that generated the polyline gotten from Directions API, then loop, starting from the index of marker A and end at the index of market B. Download the Google maps utils library with the gradle code below.

dependencies {

  // KTX for the Maps SDK for Android library
  implementation 'com.google.maps.android:maps-ktx:2.1.3'

  // KTX for the Maps SDK for Android Utility Library
  implementation 'com.google.maps.android:maps-utils-ktx:2.1.3'

  // It is recommended to also include the latest Maps SDK and/or Utility Library versions
  // as well to ensure that you have the latest features and bug fixes.
  implementation 'com.google.android.gms:play-services-maps:17.0.0'
  implementation 'com.google.maps.android:android-maps-utils:2.0.3'
}

You can then use the below method gotten from PolyUtil class of the above library to get the index of the marker point A and B:

 Polyutil.locationIndexOnEdgeOrPath(LatLng point, java.util.List<LatLng> poly, boolean closed, boolean geodesic, double toleranceEarth)

Use the following options:

  1. closed (false)
  2. geodesic (false)
  3. toleranceEarth (0.01)
Joseph Utulu
  • 236
  • 1
  • 12