latLngIsInPolygon static method

bool latLngIsInPolygon(
  1. LatLng latLng,
  2. List<LatLng> latLngList
)

判断坐标点latLng是否在多边形latLngList

Implementation

static bool latLngIsInPolygon(LatLng latLng, List<LatLng> latLngList) {
  bool isInside = false;
  int count = 0;
  double linePoint1x;
  double linePoint1y;
  double linePoint2x = 180;
  double linePoint2y;

  linePoint1x = latLng.longitude;
  linePoint1y = latLng.latitude;
  linePoint2y = latLng.latitude;

  if (latLngList.length < 3) {
    return false;
  }

  if (latLngList[0] != (latLngList[latLngList.length - 1])) {
    latLngList.add(latLngList[0]);
  }

  for (int i = 0; i < latLngList.length - 1; i++) {
    double cx1 = latLngList[i].longitude;
    double cy1 = latLngList[i].latitude;
    double cx2 = latLngList[i + 1].longitude;
    double cy2 = latLngList[i + 1].latitude;
    if (_isPointOnLine(linePoint1x, linePoint1y, cx1, cy1, cx2, cy2)) {
      return true;
    }
    if ((cy2 - cy1).abs() < ESP) {
      continue;
    }

    if (_isPointOnLine(
        cx1, cy1, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) {
      if (cy1 > cy2) {
        count++;
      }
    } else if (_isPointOnLine(
        cx2, cy2, linePoint1x, linePoint1y, linePoint2x, linePoint2y)) {
      if (cy2 > cy1) {
        count++;
      }
    } else if (_isIntersect(cx1, cy1, cx2, cy2, linePoint1x, linePoint1y,
        linePoint2x, linePoint2y)) {
      count++;
    }
  }
  if (count % POINT_ELEMENT_SIZE != 0) {
    isInside = true;
  }

  return isInside;
}