【问题标题】:How to find distance between two locations (By latitude and longitude) in kilometers如何以公里为单位查找两个位置(按纬度和经度)之间的距离
【发布时间】:2016-04-25 05:01:46
【问题描述】:

我正在开发一个 android 应用程序,它将跟踪当前位置的纬度和经度并存储在外部数据库中。这里我有一个纬度和经度列表。我使用自定义适配器填充它们。但是,这里我需要从一个基本纬度和基本经度到剩余项目纬度和经度的距离。这里的基本纬度和经度是由用户自己选择的。下面的列表解释如下我有

SELECTION         LAT        LONG         DISTANCE
-------------------------------------------------
  checkbox1    123.4546     456.48751      Text
  checkbox2    123.4546     456.48751      Text
  checkbox3    123.4546     456.48751      Text
  checkbox4    123.4546     456.48751      Text

如果用户选择复选框 1,那么我必须在 KILOMETERS 中找到从复选框 1 lat long 到复选框 2、复选框 3、check-box-4 lat long 的距离strong> 并展示在他们尊敬的位置上。 这是我编写的适配器中的一些代码,但没有显示任何结果。

 public class Locations_Adapter extends BaseAdapter {
        public String distance_string;
        Context context;
        List<Locations_modle> objects;
        double distance, latitude, longitude;
        String latitude_string, longitude_string;
        double baseLat, baseLong, finalLat, finalLong;
        Location location_pointa, location_pointb;
        TextView distance_text;
        float[] results;
        int selectedPostion = -1;

        public Locations_Adapter(Context context, int resource, List<Locations_modle> objects) {
            this.context = context;
            this.objects = objects;
        }

        /**
         * Distance calculation between two lat longs
         **/
        private static double calculateDistance(double baseLat, double baseLong, double latitude, double longitude, String unit) {
            double theta = baseLong - longitude;
            double dist = Math.sin(deg2rad(baseLat)) * Math.sin(deg2rad(latitude)) + Math.cos(deg2rad(baseLat)) * Math.cos(deg2rad(longitude)) * Math.cos(deg2rad(theta));
            dist = Math.acos(dist);
            dist = rad2deg(dist);
            dist = dist * 60 * 1.1515;
            if (unit == "K") {
                dist = dist * 1.609344;
            } else if (unit == "N") {
                dist = dist * 0.8684;
            }

            return (dist);


        }

        /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
        /*::    This function converts decimal degrees to radians                        :*/
        /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
        private static double deg2rad(double deg) {
            return (deg * Math.PI / 180.0);
        }

        /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
        /*::    This function converts radians to decimal degrees                        :*/
        /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
        private static double rad2deg(double rad) {
            return (rad * 180 / Math.PI);
        }

        @Override
        public int getCount() {
            return objects.size();
        }

        @Override
        public Object getItem(int position) {
            return objects.get(position);
        }

        @Override
        public long getItemId(int position) {
            return position;
        }

        @Override
        public View getView(final int position, View convertView, final ViewGroup parent) {

            final View locations_row = LayoutInflater.from(context).inflate(R.layout.layout_adapter_list_details, null);
            final Locations_modle location = (Locations_modle) objects.get(position);
            TextView text_cust_name = (TextView) locations_row.findViewById(R.id.txt_cust_name_heading);
            TextView latitude = (TextView) locations_row.findViewById(R.id.txt_latitude);
            latitude.setText(location.getLatitude());
            TextView longitude = (TextView) locations_row.findViewById(R.id.txt_longitude);
            distance_text = (TextView) locations_row.findViewById(R.id.txt_distance);
            longitude.setText(location.getLongitude());
            text_cust_name.setText(location.getLocationName());
            CheckBox check_locations = (CheckBox) locations_row.findViewById(R.id.check_locations);
            final Location location_point_a = new Location("Source");
            final Location location_point_b = new Location("Destination");
            location_point_a.setLatitude(Double.parseDouble(location.getLatitude()));
            location_point_a.setLongitude(Double.parseDouble(location.getLongitude()));

            if (position == selectedPostion) {
                check_locations.setChecked(true);
            } else {
                check_locations.setChecked(false);
            }
            check_locations.setOnCheckedChangeListener(new CompoundButton.OnCheckedChangeListener() {
                @Override
                public void onCheckedChanged(CompoundButton buttonView, boolean isChecked) {
                    if (isChecked) {
                        // selectedPostion = position;
                        latitude_string = location.getLatitude();
                        longitude_string = location.getLongitude();


                        baseLat = Double.parseDouble(latitude_string);
                        baseLong = Double.parseDouble(longitude_string);
                        for (int i = 0; i < objects.size(); i++) {

                            finalLat = Double.parseDouble(objects.get(i).getLatitude());
                            finalLong = Double.parseDouble(objects.get(i).getLongitude());

                            calculateDistance(baseLat, baseLong, finalLat, finalLong, "k");

                        }
distance_text.setText(Double.toString(calculateDistance(baseLat, baseLong, finalLat, finalLong, "k")));
                    } /*else {
                        selectedPostion = -1;

                    }
                    notifyDataSetChanged();

    */

                }
            });

            return locations_row;
        }
    }

谁能告诉我如何做到这一点

【问题讨论】:

    标签: android google-maps gps


    【解决方案1】:

    你可以试试这个:

    public static Double distanceBetween(LatLng point1, LatLng point2) 
    {
      if (point1 == null || point2 == null) {
          return null;
      }
      else{
        return SphericalUtil.computeDistanceBetween(point1, point2);
      }
    }
    

    【讨论】:

      【解决方案2】:

      你可以试试这个

          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();
      }
      

      【讨论】:

      • 但是如何在它们各自的文本位置显示该值。这里我的值仅在最后一个位置更新@Rathod Vijay
      【解决方案3】:

      使用这个方法:

      public static double haversineDistance(double lat1, double lng1, double lat2, double lng2) {
          final int R = 6371; // Radious of the earth
          double latDistance = toRad(lat2-lat1);
          double lonDistance = toRad(lng2-lng1);
          double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2) +
                  Math.cos(toRad(lat1)) * Math.cos(toRad(lat2)) *
                          Math.sin(lonDistance / 2) * Math.sin(lonDistance / 2);
          double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
          double distance = R * c;
          distance = distance * 1000 * 0.000621371;    //  DISTANCE IN MILES
          return (distance / 0.62137);        //  CONVERT MILES TO KM
      }
      public static double toRad(double value) {
          return value * Math.PI / 180;
      }
      

      【讨论】:

        【解决方案4】:

        你可以试试这个:

        Location loc = new Location("");
        Location p = new Location("");
        loc.setLatitude(locLat);
        loc.setLongitude(locLon);
        p.setLatitude(pLat);
        p.setLongitude(pLon);
        
        double dis = loc.distanceTo(p);
        
        String.format("%.2f", dis * 0.001);
        

        在 loc 你设置你的位置

        【讨论】:

          【解决方案5】:

          您可以使用内部 Api 查找 2 latlng 之间的距离

          public double getDistanceBetweenPoints(LatLng origin, LatLng dest, String unit) {
                      Location selected_location = new Location("Start");
              selected_location.setLatitude(origin.latitude);
              selected_location.setLongitude(origin.longitude);
          
              Location near_locations = new Location("End");
              near_locations.setLatitude(dest.latitude);
              near_locations.setLongitude(dest.longitude);
          
              //in meters
              double dist = selected_location.distanceTo(near_locations);
          
              if (unit.equals("K")) {
                  dist = dist * 1.609344;
              } else if (unit.equals("N")) {
                  dist = dist * 0.8684;
              }
          
              return dist;
          }
          

          【讨论】:

          • 但是在这里我得到了最后一个文本文件的计算距离,你能帮我如何在各自的文本位置添加距离
          • 在获取视图方法中,您可以使用所需参数调用此方法,然后将双精度转换为字符串以在文本视图中显示。 like - double dist = getDistanceBetweenPoints(LatLng origin, LatLng dest, String unit); tvDistance.setText(String.valueOf(dist));
          • 我有那个。但是在这里我想在他们尊重的行文本中显示结果值只有你能帮助我如何做到这一点@androidnoobdev
          • 从你的问题中我没有得到想要做什么?你能更详细地解释一下吗?如果可能,请添加带有一些文字的屏幕截图。
          猜你喜欢
          • 1970-01-01
          • 2021-08-21
          • 2018-03-25
          • 2010-11-28
          • 1970-01-01
          • 1970-01-01
          • 1970-01-01
          • 2017-01-05
          • 1970-01-01
          相关资源
          最近更新 更多