android - How to find distance between two locations (By latitude and longitude) in kilometers -


i developing android app track current location latitude , longitude , store in external database.here having list of latitudes , longitudes.i populated them using custom adapter . but,here need distance 1 base latitude , base longitude remaining items latitude , longitude.here base latitude , longitude selected user self.here below list explains 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 check-box 1 have find distance check-box 1 lat long check-box 2,check-box 3,check-box-4 lat long in kilometers , display in respected position . code adapter had written not showing 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 2 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);           }          /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/         /*::    function converts decimal degrees radians                        :*/         /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/         private static double deg2rad(double deg) {             return (deg * math.pi / 180.0);         }          /*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/         /*::    function converts radians 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);                         (int = 0; < 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 1 tell how 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);   } } 

Comments

Popular posts from this blog

javascript - How to get current YouTube IDs via iMacros? -

c# - Maintaining a program folder in program files out of date? -

emulation - Android map show my location didn't work -