【问题标题】:Find if user is within given distance of given Coords查找用户是否在给定坐标的给定距离内
【发布时间】:2016-12-01 08:36:51
【问题描述】:

注意:我不需要持续监控用户位置,只需单击按钮执行一次检查

我有一组纬度和经度,需要检查用户是否在该点的给定距离内,我该怎么做?我检查了Geofencing,但它像服务一样工作,而我需要在主线程中执行一次检查

【问题讨论】:

  • 您可以计算两个坐标之间的距离并相应地工作

标签: android geolocation location geofencing


【解决方案1】:

只需检查一下。

对于具有经纬度设置边界的位置,使用以下代码:

public LatLngBounds getLocationBounds(LatLng sourceLocation, double distance) 
{
  LatLng southwest = SphericalUtil.computeOffset(sourceLocation, distance, 225);
   LatLng northeast = SphericalUtil.computeOffset(sourceLocation, distance, 45);
    return new LatLngBounds(southwest, northeast);
}

【讨论】:

    【解决方案2】:

    这是用于计算两点之间距离的方法:

    public double CalculationByDistance(LatLng StartP, LatLng EndP) {
            int Radius = 6371;// radius of earth in Km
            double lat1 = StartP.latitude;
            double lat2 = EndP.latitude;
            double lon1 = StartP.longitude;
            double lon2 = EndP.longitude;
            double dLat = Math.toRadians(lat2 - lat1);
            double dLon = Math.toRadians(lon2 - lon1);
            double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
                    + Math.cos(Math.toRadians(lat1))
                    * Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
                    * Math.sin(dLon / 2);
            double c = 2 * Math.asin(Math.sqrt(a));
            double valueResult = Radius * c;
            double km = valueResult / 1;
            DecimalFormat newFormat = new DecimalFormat("####");
            int kmInDec = Integer.valueOf(newFormat.format(km));
            double meter = valueResult % 1000;
            int meterInDec = Integer.valueOf(newFormat.format(meter));
            Log.i("Radius Value", "" + valueResult + "   KM  " + kmInDec
                    + " Meter   " + meterInDec);
    
            return Radius * c;
        }
    

    这也是这个问题的重复:Find distance between two points on map using Google Map API V2

    请在以后询问之前尝试查看您的问题是否已得到解答。

    【讨论】:

    • 我实际上并没有使用地图,只是静态检查..也许这就是我没有偶然发现这个问题的原因
    • 没问题,这仍然会为您提供所需的功能。只需调用此方法并传递您的 Lat/Lng 值,然后根据您需要的任何距离检查此方法返回的值并相应地继续。
    猜你喜欢
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 1970-01-01
    • 2016-01-05
    • 2010-10-27
    相关资源
    最近更新 更多