0

I am creating a simple navigation application using GoogleMap. When the user set the start location and destination location I am drawing the path using polylines.

If the user deviate from the path, I want to redraw the path based on current user location.

Problem I have is how to detect user is not in the current route?

I am using https://maps.googleapis.com/maps/api/directions/json? to get the directions. Is there a function in that to detect whether user is outside the route?

I also have the list of coordinates used to draw the poly lines. Can they be used to detect user moved outside the current route ?

Based on an example I found in web, I created this method. The example is not designed to for the map coordinates. Function for finding the distance between a point and an edge in java

Is there a better way of doing this?

static double perpendicularDistance(LatLng point, LatLng start, LatLng end) {
double x = point.longitude;
double y = point.latitude;

double x1 = start.longitude;
double y1 = start.latitude;
double x2 = end.longitude;
double y2 = end.latitude;

double A = x - x1;
double B = y - y1;
double C = x2 - x1;
double D = y2 - y1;

double dot = A * C + B * D;
double len_sq = C * C + D * D;
double param = -1;
if (len_sq != 0) //in case of 0 length line
  param = dot / len_sq;

double xx, yy;

if (param < 0) {
  xx = x1;
  yy = y1;
} else if (param > 1) {
  xx = x2;
  yy = y2;
} else {
  xx = x1 + param * C;
  yy = y1 + param * D;
}

var dx = x - xx;
var dy = y - yy;
// one degree is equivalent to 111km approximately
return (sqrt(dx * dx + dy * dy)).abs() * 111000;

}

Janaka
  • 2,505
  • 4
  • 33
  • 57

2 Answers2

1

May be you need to get coordinates of user everytime he/she walks, and check if that coordinates lies in the list of coordinates which you have. You can get location using https://pub.dev/packages/location. These is just a thought, I can be wrong.

Yaseen
  • 214
  • 1
  • 11
  • I am already using the location. But how do I check current coordinates "lies in the list of coordinates". Is there a formula for this? – Janaka Feb 07 '22 at 09:55
  • https://pub.dev/packages/dart_jts might have the specific functionality to buffer the polyline returned from the Google Maps API and then do a point in polygon(buffered polyline) to see if the user has deviated. – Justin Poehnelt Feb 07 '22 at 16:17
  • @jpoehnelt we do have the list of points. we can construct a list of lines also. Need a way to check a point is say within 50m from the line. Or something similar to that. – Janaka Feb 08 '22 at 06:49
  • you can use the formula from a point to a straight, check out https://www.toppr.com/guides/maths/straight-lines/distance-of-point-from-a-line/, you have to correct some things because you will use coordinates instead of just points in a cartesian plane – Williams Bobadilla Feb 18 '22 at 19:41
1

You can use this package

https://pub.dev/packages/google_maps_utils

with this method to check if you are not near of your route:

    /// Computes whether the given point lies on or near a polyline, within a specified
  /// tolerance in meters. The polyline is composed of great circle segments if geodesic
  /// is true, and of Rhumb segments otherwise. The polyline is not closed -- the closing
  /// segment between the first point and the last point is not included.
  static bool isLocationOnPathTolerance(Point point, List<Point> polyline, 
                                        bool geodesic, double tolerance
                                        )

Where point is, a class to represent two dimensional positions, for example;

var rightBottom = const Point(200, 400);

As you can see, you can send the tolerance for the distance in meters.