簡體   English   中英

檢測用戶在 GoogleMap 路線之外(到路徑的垂直距離)

[英]Detect user is outside the GoogleMap route (perpendicular distance to path)

我正在使用 GoogleMap 創建一個簡單的導航應用程序。 當用戶設置開始位置和目標位置時,我正在使用折線繪制路徑。

如果用戶偏離路徑,我想根據當前用戶位置重繪路徑。

我的問題是如何檢測用戶不在當前路線中

我正在使用https://maps.googleapis.com/maps/api/directions/json 獲取方向。 是否有一個 function 來檢測用戶是否在路線之外?

我還有用於繪制折線的坐標列表。 它們可以用來檢測用戶移動到當前路線之外嗎?

基於我在 web 中找到的示例,我創建了此方法。 該示例不適用於 map 坐標。 Function 用於查找 java 中的點和邊之間的距離

有沒有更好的方法來做到這一點?

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;

}

可能您每次走路時都需要獲取用戶的坐標,並檢查該坐標是否在您擁有的坐標列表中。 您可以使用https://pub.dev/packages/location獲取位置。 這些只是一個想法,我可能是錯的。

您可以使用此 package

https://pub.dev/packages/google_maps_utils

使用此方法檢查您是否不在您的路線附近:

    /// 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
                                        )

其中point是,例如一個class來表示二維位置;

var rightBottom = const Point(200, 400);

如您所見,您可以發送以米為單位的距離容差。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM