Reputation: 73
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
Upvotes: 2
Views: 1006
Reputation: 6067
You can use internal Api to find distance between 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;
}
Upvotes: 0
Reputation: 173
You can try this:
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);
In loc you set your location
Upvotes: 0
Reputation: 1226
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);
}
}
Upvotes: 1
Reputation: 6179
Use this method:
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;
}
Upvotes: 0
Reputation: 417
You can try this
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();
}
Upvotes: 0