如何找到两个位置之间的距离(按纬度和经度),以千米为单位

时间:2016-04-25 05:01:46

标签: android google-maps gps

我正在开发一个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的距离强>并显示在他们尊重的位置。 这是我编写的适配器的一些代码,但它没有显示任何结果。

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

任何人都可以告诉你如何实现这个

5 个答案:

答案 0 :(得分:1)

你可以试试这个:

portion

答案 1 :(得分:0)

你可以试试这个

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

答案 2 :(得分:0)

使用此方法:

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

答案 3 :(得分:0)

你可以试试这个:

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中设置您的位置

答案 4 :(得分:0)

您可以使用内部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;
}