【问题标题】:Getting locations near User's Location获取用户位置附近的位置
【发布时间】:2017-03-01 09:10:28
【问题描述】:

我有一组经度和纬度的地标。 如何计算地标与用户位置之间的距离以获取例如距离用户位置 10 公里半径内的地标

【问题讨论】:

    标签: android location google-location-services


    【解决方案1】:
    private static double distanceInKm(double lat1, double lon1, double lat2, double lon2) {
            int R = 6371; // km
            double x = (lon2 - lon1) * Math.cos((lat1 + lat2) / 2);
            double y = (lat2 - lat1);
            return (Math.sqrt(x * x + y * y) * R) / 1000;
        }
    

    或者

    Location location1 = new Location("");
    location1.setLatitude(latitude1);
    location1.setLongitude(longitude1);
    
    Location location2 = new Location("");
    location2.setLatitude(latitude2);
    location2.setLongitude(longitude2);
    
    float distanceInKm = (location1.distanceTo(location2))/1000;
    

    【讨论】:

      【解决方案2】:

      如果您有当前位置 LatLong 点,那么您可以使用以下代码计算到 LatLongs 之间的距离。

      public float distance (float lat_a, float lng_a, float lat_b, float lng_b)
      {
      double earthRadius = 3958.75;
      double latDiff = Math.toRadians(lat_b-lat_a);
      double lngDiff = Math.toRadians(lng_b-lng_a);
      double a = Math.sin(latDiff /2) * Math.sin(latDiff /2) +
      Math.cos(Math.toRadians(lat_a)) * Math.cos(Math.toRadians(lat_b)) *
      Math.sin(lngDiff /2) * Math.sin(lngDiff /2);
      double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
      double distance = earthRadius * c;
      
      int meterConversion = 1609;
      
      return new Float(distance * meterConversion).floatValue();
      }
      

      【讨论】:

        猜你喜欢
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 1970-01-01
        • 2011-06-14
        • 2023-03-17
        • 2012-02-28
        • 1970-01-01
        • 2016-05-08
        相关资源
        最近更新 更多