Android 中的 GPS 区域

发布于 2024-10-25 07:47:07 字数 19 浏览 3 评论 0原文

有没有GPS区域计算类?

Is there any class for GPS area calculate?

如果你对这篇内容有疑问,欢迎到本站社区发帖提问 参与讨论,获取更多帮助,或者扫码二维码加入 Web 技术交流群。

扫码二维码加入Web技术交流群

发布评论

需要 登录 才能够评论, 你可以免费 注册 一个本站的账号。

评论(2

月野兔 2024-11-01 07:47:07

我使用此代码通过 Android 计算由 GPS 点分隔的区域:

  private static final double EARTH_RADIUS = 6371000;// meters

  public static double calculateAreaOfGPSPolygonOnEarthInSquareMeters(final List<Location> locations) {
    return calculateAreaOfGPSPolygonOnSphereInSquareMeters(locations, EARTH_RADIUS);
  }

  private static double calculateAreaOfGPSPolygonOnSphereInSquareMeters(final List<Location> locations, final double radius) {
    if (locations.size() < 3) {
      return 0;
    }

    final double diameter = radius * 2;
    final double circumference = diameter * Math.PI;
    final List<Double> listY = new ArrayList<Double>();
    final List<Double> listX = new ArrayList<Double>();
    final List<Double> listArea = new ArrayList<Double>();
    // calculate segment x and y in degrees for each point
    final double latitudeRef = locations.get(0).getLatitude();
    final double longitudeRef = locations.get(0).getLongitude();
    for (int i = 1; i < locations.size(); i++) {
      final double latitude = locations.get(i).getLatitude();
      final double longitude = locations.get(i).getLongitude();
      listY.add(calculateYSegment(latitudeRef, latitude, circumference));
      Log.d(LOG_TAG, String.format("Y %s: %s", listY.size() - 1, listY.get(listY.size() - 1)));
      listX.add(calculateXSegment(longitudeRef, longitude, latitude, circumference));
      Log.d(LOG_TAG, String.format("X %s: %s", listX.size() - 1, listX.get(listX.size() - 1)));
    }

    // calculate areas for each triangle segment
    for (int i = 1; i < listX.size(); i++) {
      final double x1 = listX.get(i - 1);
      final double y1 = listY.get(i - 1);
      final double x2 = listX.get(i);
      final double y2 = listY.get(i);
      listArea.add(calculateAreaInSquareMeters(x1, x2, y1, y2));
      Log.d(LOG_TAG, String.format("area %s: %s", listArea.size() - 1, listArea.get(listArea.size() - 1)));
    }

    // sum areas of all triangle segments
    double areasSum = 0;
    for (final Double area : listArea) {
      areasSum = areasSum + area;
    }

    // get abolute value of area, it can't be negative
    return Math.abs(areasSum);// Math.sqrt(areasSum * areasSum);
  }

  private static Double calculateAreaInSquareMeters(final double x1, final double x2, final double y1, final double y2) {
    return (y1 * x2 - x1 * y2) / 2;
  }

  private static double calculateYSegment(final double latitudeRef, final double latitude, final double circumference) {
    return (latitude - latitudeRef) * circumference / 360.0;
  }

  private static double calculateXSegment(final double longitudeRef, final double longitude, final double latitude,
      final double circumference) {
    return (longitude - longitudeRef) * circumference * Math.cos(Math.toRadians(latitude)) / 360.0;
  }

您可以轻松地将其适应 java,不使用位置对象列表,而是使用两个纬度和经度列表。

这是基于以下公式:

http://mathworld.wolfram.com/PolygonArea.html

此处描述:

http://en.wikipedia.org/wiki/Polygon

的原理使用以下公式计算多边形面积:

http:// maruzar.blogspot.com/2011/12/irregular-and-regular-polygon-area-by.html

适用于球体面积的公式,在我们的例子中是世界球体,和 excel 计算示例对于验证 Java 或其他语言的算法非常有用:

I use this code to calculate an area delimited by GPS points with Android:

  private static final double EARTH_RADIUS = 6371000;// meters

  public static double calculateAreaOfGPSPolygonOnEarthInSquareMeters(final List<Location> locations) {
    return calculateAreaOfGPSPolygonOnSphereInSquareMeters(locations, EARTH_RADIUS);
  }

  private static double calculateAreaOfGPSPolygonOnSphereInSquareMeters(final List<Location> locations, final double radius) {
    if (locations.size() < 3) {
      return 0;
    }

    final double diameter = radius * 2;
    final double circumference = diameter * Math.PI;
    final List<Double> listY = new ArrayList<Double>();
    final List<Double> listX = new ArrayList<Double>();
    final List<Double> listArea = new ArrayList<Double>();
    // calculate segment x and y in degrees for each point
    final double latitudeRef = locations.get(0).getLatitude();
    final double longitudeRef = locations.get(0).getLongitude();
    for (int i = 1; i < locations.size(); i++) {
      final double latitude = locations.get(i).getLatitude();
      final double longitude = locations.get(i).getLongitude();
      listY.add(calculateYSegment(latitudeRef, latitude, circumference));
      Log.d(LOG_TAG, String.format("Y %s: %s", listY.size() - 1, listY.get(listY.size() - 1)));
      listX.add(calculateXSegment(longitudeRef, longitude, latitude, circumference));
      Log.d(LOG_TAG, String.format("X %s: %s", listX.size() - 1, listX.get(listX.size() - 1)));
    }

    // calculate areas for each triangle segment
    for (int i = 1; i < listX.size(); i++) {
      final double x1 = listX.get(i - 1);
      final double y1 = listY.get(i - 1);
      final double x2 = listX.get(i);
      final double y2 = listY.get(i);
      listArea.add(calculateAreaInSquareMeters(x1, x2, y1, y2));
      Log.d(LOG_TAG, String.format("area %s: %s", listArea.size() - 1, listArea.get(listArea.size() - 1)));
    }

    // sum areas of all triangle segments
    double areasSum = 0;
    for (final Double area : listArea) {
      areasSum = areasSum + area;
    }

    // get abolute value of area, it can't be negative
    return Math.abs(areasSum);// Math.sqrt(areasSum * areasSum);
  }

  private static Double calculateAreaInSquareMeters(final double x1, final double x2, final double y1, final double y2) {
    return (y1 * x2 - x1 * y2) / 2;
  }

  private static double calculateYSegment(final double latitudeRef, final double latitude, final double circumference) {
    return (latitude - latitudeRef) * circumference / 360.0;
  }

  private static double calculateXSegment(final double longitudeRef, final double longitude, final double latitude,
      final double circumference) {
    return (longitude - longitudeRef) * circumference * Math.cos(Math.toRadians(latitude)) / 360.0;
  }

You can easily adapt it to java not using a list of location objects but two list of latitudes and longitudes.

This is based on this formula:

http://mathworld.wolfram.com/PolygonArea.html

also described here:

http://en.wikipedia.org/wiki/Polygon

Principle of ploygon area calculation with this formula:

http://maruzar.blogspot.com/2011/12/irregular-and-regular-polygon-area-by.html

Formula adapted to area on a sphere, in our case the world sphere, and excel example of the calculation very usefull to verify your algorithm in Java or other language:

http://maruzar.blogspot.com/2012/03/calculating-land-lot-area-with-gps.html

伪心 2024-11-01 07:47:07

您可以通过记录三个点来计算两个向量的平行四边形的面积:
1) 起点A
2)B点
3) C点

得到两个向量AB和AC [B(x,y)-A(x,y) = AB(x,y)]

面积为|| AB X AC || !

you can calculate area of the resulting parallelogram of two vectors by recording three points:
1) starting point A
2) point B
3) point C

Get the two vectors AB and AC [B(x,y)-A(x,y) = AB(x,y)]

Area is || AB X AC || !

~没有更多了~
我们使用 Cookies 和其他技术来定制您的体验包括您的登录状态等。通过阅读我们的 隐私政策 了解更多相关信息。 单击 接受 或继续使用网站,即表示您同意使用 Cookies 和您的相关数据。
原文