How to find the distance between two locations (latitude and longitude) in kilometers

advertisements

I am developing an android app which will track the current location latitude and longitude and store in external database.Here i am having a list of latitudes and longitudes.I populated them by using the custom adapter . But,Here i need the distance from one base latitude and base longitude to remaining items latitude and longitude.Here the base latitude and longitude was selected by the user it self.Here the below the list explains like this i have

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

If user selects the check-box 1 then i have to find the distance from check-box 1 lat long to check-box 2,check-box 3,check-box-4 lat long in KILOMETERS and display in their respected position . This is some code from adapter i had written but it was not showing any results.

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

Can any one tell how to achieve this


You can try this:

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