您好, 欢迎来到 !    登录 | 注册 | | 设为首页 | 收藏本站

除了Android API之外,是否还有其他用于计算Geofence违规的API

除了Android API之外,是否还有其他用于计算Geofence违规的API

您可以自己实现它,而无需使用任何框架,这非常容易…

我假设您要检查您是否在圆形地理围栏内。

为此,只需计算圆心与您的位置之间的距离(经度,纬度)。如果距离小于圆半径,则说明您在地理围栏内,否则就在地理围栏外。

像这样:

    boolean checkInside(Circle circle, double longitude, double latitude) {
        return calculateDistance(
            circle.getLongitude(), circle.getLatitude(), longitude, latitude
        ) < circle.geTradius();}

要计算两点之间的距离,可以使用以下方法

double calculateDistance(
  double longitude1, double latitude1, 
  double longitude2, double latitude2) {
    double c = 
        Math.sin(Math.toradians(latitude1)) *
        Math.sin(Math.toradians(latitude2)) +
            Math.cos(Math.toradians(latitude1)) *
            Math.cos(Math.toradians(latitude2)) *
            Math.cos(Math.toradians(longitude2) - 
                Math.toradians(longitude1));
    c = c > 0 ? Math.min(1, c) : Math.max(-1, c);
    return 3959 * 1.609 * 1000 * Math.acos(c);
}

该公式称为haversine公式。它考虑了地球的弯曲。结果以米为单位。

我也在博客上描述了它:

用于检查地理围栏圆(还描述了两点之间的距离计算):http : //stefanbangels.blogspot.be/2014/03/point-geo-fencing-sample-code.html

用于检查地理栅栏多边形:http ://stefanbangels.blogspot.be/2013/10/geo-fencing-sample-code.html

其他 2022/1/1 18:26:57 有507人围观

撰写回答


你尚未登录,登录后可以

和开发者交流问题的细节

关注并接收问题和回答的更新提醒

参与内容的编辑和改进,让解决方法与时俱进

请先登录

推荐问题


联系我
置顶