Hi I have been doing the app for calculating the distance and speed of travelling. All the function of getting speed and distance working fine, however during the testing I saw the bug where the distance is getting started to be measured only if the speed is 19 Km/h +
Would anybody have any idea why this might be happening ?
Thanks
//initialize location listener
locationListener = new LocationListener() {
#Override
public void onLocationChanged(Location location) {
getSpeed(location);
getDistance(location);
}
#Override
public void onStatusChanged(String s, int i, Bundle bundle) {
}
#Override
public void onProviderEnabled(String s) {
}
#Override
public void onProviderDisabled(String s) {
}
//get the speed from the given location updates
public void getSpeed(Location location) {
currentSpeed = (location.getSpeed() * 3600 / 1000);
String convertedSpeed = String.format("%.2f", currentSpeed);
speedTxt.setText(convertedSpeed + "Km/h");
}
private void getDistance(Location location) {
//to capture current location and keep as starting position of person
if (pLat == 500.0 && pLng == 500.0 ){
pLat = location.getLatitude();
pLng = location.getLongitude();
}
if (cLat == 500.0 && cLng == 500.0){
cLat = location.getLatitude();
cLng=location.getLongitude();
}
//to check is the person has changed location
if (pLat != cLat && pLng != cLng) {
pLat = cLat;
pLng = cLng;
}
//update the current location
cLat = location.getLatitude();
cLng = location.getLongitude();
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
String convertedDistance = String.format("%.2f", distance);
distanceTxt.setText(" " + convertedDistance);
}
public double getDistanceBetweenGeoPoints(Double cLat, Double cLng, Double pLat, Double pLng) {
// CALCULATE DISTANCE BETWEEN TWO POINTS
double earthRadius = 6367; //meters
double dLat = Math.toRadians(cLat - pLat);
double dLng = Math.toRadians(cLng - pLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(cLat)) * Math.cos(Math.toRadians(pLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = (double) (earthRadius * c);
dist = Math.round(dist * 100) / 100.0;
return dist;
}
};
Your getDistanceBetweenGeoPoints function is going to return exactly 0 for small differences in location because of this line:
dist = Math.round(dist * 100) / 100.0;
Try rounding the total distance only for display purposes in GetDistance():
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
String convertedDistance = String.format("%.2f", Math.round(dist * 100) / 100.0);
distanceTxt.setText(" " + convertedDistance);
private void getDistance(Location location) {
//to capture current location and keep as starting position of person
if (pLat == 500.0 && pLng == 500.0) {
pLat = location.getLatitude();
pLng = location.getLongitude();
}
if (cLat == 500.0 && cLng == 500.0) {
cLat = location.getLatitude();
cLng = location.getLongitude();
}
//to check is the person has changed location
if (pLat != cLat && pLng != cLng) {
pLat = cLat;
pLng = cLng;
}
//update the current location
cLat = location.getLatitude();
cLng = location.getLongitude();
//call the calculation method
distance += getDistanceBetweenGeoPoints(cLat, cLng, pLat, pLng);
//String convertedDistance = String.format("%.2f", distance);
String convertedDistance = String.format("%.2f", Math.round(distance * 100) / 100.0);
distanceTxt.setText(" " + convertedDistance);
}
public double getDistanceBetweenGeoPoints(Double cLat, Double cLng, Double pLat, Double pLng) {
// CALCULATE DISTANCE BETWEEN TWO POINTS
double earthRadius = 6367; //meters
double dLat = Math.toRadians(cLat - pLat);
double dLng = Math.toRadians(cLng - pLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) +
Math.cos(Math.toRadians(cLat)) * Math.cos(Math.toRadians(pLat)) *
Math.sin(dLng / 2) * Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = (double) (earthRadius * c);
//dist = Math.round(dist * 100) / 100.0;
return dist;
}
Related
I am new in Java and Firebase. Now I have try to using the geolocation of two location that get from database and calculate the distance in km using the geolocation formula.
The coding as shown below.
private void getUserLocation() {
DatabaseReference databaseReference = FirebaseDatabase.getInstance().getReference("UserID")
.child(FirebaseAuth.getInstance().getCurrentUser().getUid()).child("User location");
ValueEventListener listener = databaseReference.addValueEventListener(new ValueEventListener() {
#Override
public void onDataChange(#NonNull DataSnapshot dataSnapshot) {
userlatitude = dataSnapshot.child("latitude").getValue(String.class);
userlongitude = dataSnapshot.child("longitude").getValue(String.class);
Double UserLatitude = Double.parseDouble(userlatitude);
Double UserLongitude = Double.parseDouble(userlongitude);
//String Userlatitudedouble = String.valueOf(UserLatitude);
//String Userlongitudedouble = String.valueOf(UserLongitude);
Log.i("user double latitude", userlatitude);
Log.i("User double longitude", userlongitude);
Double hostlatitudedouble = Double.parseDouble(latitude_host);
Double hostlongitudedouble = Double.parseDouble(longitude_host);
//String hostlatitudedouble = String.valueOf(HostLatitude);
//String hostlongitudedouble = String.valueOf(HostLongitude);
Log.i("host double latitude", latitude_host);
Log.i("host double longitude", longitude_host);
getDistance(UserLatitude, UserLongitude, hostlatitudedouble, hostlongitudedouble);
}
#Override
public void onCancelled(#NonNull DatabaseError error) {
}
});
}
private double rad2deg(double distance) {
return (distance * 180.0/Math.PI);
}
private double deg2rad(double lat1) {
return (lat1 * Math.PI / 180.0);
}
private void getDistance(double lat1, double long1, double lat2, double long2) {
double longdiff = long1 - long2;
double distance = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
+ Math.cos(deg2rad(longdiff));
distance = Math.acos(distance);
String testing= String.valueOf(distance);
Log.i("testing0" , testing);
distance = rad2deg(distance);
distance = distance *60 * 1.1515 * 1.609344; //distance in km
String distances = String.valueOf(distance);
Log.i("distance = ", distances);
Toast.makeText(this, "Distance = " + distances, Toast.LENGTH_SHORT).show();
}
Now I am sure that I am success to get the latitude and longitude of two places from Firebase. However, I dont know why I keep get the result of NaN for the calculation. Guys, please help.
use this function for calculating the distance of two location. I used that in my last project and I'm sure about that:
public static double calculateDistance(LatLng pointA, LatLng pointB) {
double earthRadius = 6371;
double latDiff = Math.toRadians(pointB.getLatitude() - pointA.getLatitude());
double lngDiff = Math.toRadians(pointB.getLongitude() - pointA.getLongitude());
double a = Math.sin(latDiff / 2) * Math.sin(latDiff / 2) +
Math.cos(Math.toRadians(pointA.getLatitude())) *
Math.cos(Math.toRadians(pointB.getLatitude())) *
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 = 1000;
return distance * meterConversion;
}
I know how to find the distance between two location on android studio google map but how to calculate which distance is the nearest one compering with 3 or 4 found location?
public void onDirectionFinderSuccess(List<Route> routes) {
progressDialog.dismiss();
polylinePaths = new ArrayList<>();
originMarkers = new ArrayList<>();
destinationMarkers = new ArrayList<>();
for (Route route : routes) {
mMap.moveCamera(CameraUpdateFactory.newLatLngZoom(route.startLocation, 16));
((TextView) findViewById(R.id.tvDuration)).setText(route.duration.text);
((TextView) findViewById(R.id.tvDistance)).setText(route.distance.text);
originMarkers.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.start_blue))
.title(route.startAddress)
.position(route.startLocation)));
destinationMarkers.add(mMap.addMarker(new MarkerOptions()
.icon(BitmapDescriptorFactory.fromResource(R.drawable.end_green))
.title(route.endAddress)
.position(route.endLocation)));
PolylineOptions polylineOptions = new PolylineOptions().
geodesic(true).
color(Color.BLUE).
width(10);
for (int i = 0; i < route.points.size(); i++)
polylineOptions.add(route.points.get(i));
polylinePaths.add(mMap.addPolyline(polylineOptions));
}
}
If you want to calculate the distance,you can calculate like this way
private double caldistance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1)) * Math.sin(deg2rad(lat2)) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) * Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
Calculate the distance for all found locations and having smallest distance will be the nearest location.
public double distance(double lat1, double lat2, double lon1,
double lon2, double el1, double el2) {
final int R = 6371; // Radius of the earth
Double latDistance = Math.toRadians(lat2 - lat1);
Double lonDistance = Math.toRadians(lon2 - lon1);
Double a = Math.sin(latDistance / 2) * Math.sin(latDistance / 2)
+ Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(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 * 1000; // convert to meters
double height = el1 - el2;
distance = Math.pow(distance, 2) + Math.pow(height, 2);
return Math.sqrt(distance);
}
If you want to get all the nearest locations
Create two Locations like this:
Location source = new Location("");
Location destination = new Location("");
source.setLatitude(src_lat);
source.setLongitude(src_lng);
destination.setLatitude(dest_lat);
destination.setLongitude(dest_lng);
if(source.distanceTo(destination)<3000){ // 3000 meters = 3 km
// do something ...
}
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);
}
}
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();
}
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;
}
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
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;
}
Look at this example:
public void start(){
//...
locationManager.requestLocationUpdates(LocationManager.GPS_PROVIDER, TEN_SECONDS, TEN_METERS, this);
}
#Override
public void onLocationChanged(Location location) {
if(location.distanceTo(_lastLocation) > TEN_KM_IN_METERS){
actionA(location);
_lastLocation = location;
} else {
actionB(location);
}
}
The implementation of Location#distanceTo(l) is pretty complicated and CPU-intensive. So i don't want to call this operation on every location update.
Question: is it any proper way to avoid unnecessary Location#distanceTo(l) calls
What i tried so far. According Wiki - Decimal degrees i do it that way:
private boolean closeTogether(Location a, Location b) {
double changeLat = Math.abs(a.getLatitude() - b.getLatitude());
final float myNaiveMax = 0.005;
if (changeLat > myNaiveMax) {
return false;
}
double changeLon = Math.abs(a.getLongitude() - b.getLongitude());
if (changeLon > myNaiveMax) {
return false;
}
return true;
}
#Override
public void onLocationChanged(Location location) {
if(!closeTogether(location, _lastLocation) && location.distanceTo(_lastLocation) > TEN_KM_IN_METERS){
actionA(location);
_lastLocation = location;
} else {
actionB(location);
}
}
I've found that the Haversine formula is very good for this. Works well for my delivery tracking application. Here's how I calculate the distance between two points. Should get you started :)
/**
* getDistanceBetweenTwoPoints
* #param p1 - First point
* #param p2 - Second point
* #return distance between the two specified points (as the crow flys)
*/
public static double getDistanceBetweenTwoPoints(PointF p1, PointF p2) {
double R = 6371000; // Earth radius
double dLat = Math.toRadians(p2.x - p1.x);
double dLon = Math.toRadians(p2.y - p1.y);
double lat1 = Math.toRadians(p1.x);
double lat2 = Math.toRadians(p2.x);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2)
* Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double d = R * c;
return d;
}
Edit And another
public static PointF calculateDerivedPosition(PointF point,
double range, double bearing)
{
double EarthRadius = 6371000; // m
double latA = Math.toRadians(point.x);
double lonA = Math.toRadians(point.y);
double angularDistance = range / EarthRadius;
double trueCourse = Math.toRadians(bearing);
double lat = Math.asin(Math.sin(latA) * Math.cos(angularDistance) +
Math.cos(latA) * Math.sin(angularDistance) * Math.cos(trueCourse));
double dlon = Math.atan2(Math.sin(trueCourse) * Math.sin(angularDistance) * Math.cos(latA),
Math.cos(angularDistance) - Math.sin(latA) * Math.sin(lat));
double lon = ((lonA + dlon + Math.PI) % (Math.PI * 2)) - Math.PI;
lat = Math.toDegrees(lat);
lon = Math.toDegrees(lon);
PointF newPoint = new PointF((float) lat, (float) lon);
return newPoint;
}
Here my code I used below code to calculate the distance between two location using their latitude and longitude. It is giving wrong distance. sometimes getting right and sometimes getting irrelevant distance.
We are getting lat1 and lng1 from database.
//getting lat2 and lng2 from GPS as below
public class MyLocationListener implements LocationListener {
#Override
public void onLocationChanged(Location loc)
{
lat2=loc.getLatitude();
lng2=loc.getLongitude();
String Text = "My current location is: " +"Latitud = "+ loc.getLatitude() +"Longitud = " + loc.getLongitude();
//System.out.println("Lat & Lang form Loc"+Text);
//Toast.makeText( getApplicationContext(), Text,Toast.LENGTH_SHORT).show();
}
#Override
public void onProviderDisabled(String provider)
{
}
#Override
public void onProviderEnabled(String provider)
{
}
#Override
public void onStatusChanged(String provider, int status, Bundle extras)
{
}
//Calculating distance
double earthRadius = 3958.75;
double dLat = Math.toRadians(lat1-lat2);
double dLng = Math.toRadians(lng1-lng2);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat2)) * Math.cos(Math.toRadians(lat1)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double dist = earthRadius * c;
There is an android.location.Location.distanceBetween() method which does this quite well.
Android Developer Docs: Location
Here getting distance in miles (mi)
private double distance(double lat1, double lon1, double lat2, double lon2) {
double theta = lon1 - lon2;
double dist = Math.sin(deg2rad(lat1))
* Math.sin(deg2rad(lat2))
+ Math.cos(deg2rad(lat1))
* Math.cos(deg2rad(lat2))
* Math.cos(deg2rad(theta));
dist = Math.acos(dist);
dist = rad2deg(dist);
dist = dist * 60 * 1.1515;
return (dist);
}
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
Try this code.
startPoint.distanceTo(endPoint) function returns the distance between those places in meters.
Location startPoint=new Location("locationA");
startPoint.setLatitude(17.372102);
startPoint.setLongitude(78.484196);
Location endPoint=new Location("locationA");
endPoint.setLatitude(17.375775);
endPoint.setLongitude(78.469218);
double distance=startPoint.distanceTo(endPoint);
here "distance" is our required result in Meters. I hope it will work for android.
in build.gradle:
compile 'com.google.maps.android:android-maps-utils:0.4'
and then:
public static Double distanceBetween(LatLng point1, LatLng point2) {
if (point1 == null || point2 == null) {
return null;
}
return SphericalUtil.computeDistanceBetween(point1, point2);
}
If you have two Location Objects Location loc1 and Location loc2 you do
float distance = loc1.distanceTo(loc2);
If you have longitude and latitude values you use the static distanceBetween() function
float[] results = new float[1];
Location.distanceBetween(startLatitude, startLongitude,
endLatitude, endLongitude, results);
float distance = results[0];
private String getDistanceOnRoad(double latitude, double longitude,
double prelatitute, double prelongitude) {
String result_in_kms = "";
String url = "http://maps.google.com/maps/api/directions/xml?origin="
+ latitude + "," + longitude + "&destination=" + prelatitute
+ "," + prelongitude + "&sensor=false&units=metric";
String tag[] = { "text" };
HttpResponse response = null;
try {
HttpClient httpClient = new DefaultHttpClient();
HttpContext localContext = new BasicHttpContext();
HttpPost httpPost = new HttpPost(url);
response = httpClient.execute(httpPost, localContext);
InputStream is = response.getEntity().getContent();
DocumentBuilder builder = DocumentBuilderFactory.newInstance()
.newDocumentBuilder();
Document doc = builder.parse(is);
if (doc != null) {
NodeList nl;
ArrayList args = new ArrayList();
for (String s : tag) {
nl = doc.getElementsByTagName(s);
if (nl.getLength() > 0) {
Node node = nl.item(nl.getLength() - 1);
args.add(node.getTextContent());
} else {
args.add(" - ");
}
}
result_in_kms = String.format("%s", args.get(0));
}
} catch (Exception e) {
e.printStackTrace();
}
return result_in_kms;
}
In Kotlin
private fun distanceInMeter(startLat: Double, startLon: Double, endLat: Double, endLon: Double): Float {
var results = FloatArray(1)
Location.distanceBetween(startLat,startLon,endLat,endLon,results)
return results[0]
}
private float distanceFrom_in_Km(float lat1, float lng1, float lat2, float lng2) {
if (lat1== null || lng1== null || lat2== null || lng2== null)
{
return null;
}
double earthRadius = 6371000; //meters
double dLat = Math.toRadians(lat2-lat1);
double dLng = Math.toRadians(lng2-lng1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLng/2) * Math.sin(dLng/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
float dist = (float) (earthRadius * c);
return dist;
}
Use the below method for calculating the distance of two different locations.
public double getKilometers(double lat1, double long1, double lat2, double long2) {
double PI_RAD = Math.PI / 180.0;
double phi1 = lat1 * PI_RAD;
double phi2 = lat2 * PI_RAD;
double lam1 = long1 * PI_RAD;
double lam2 = long2 * PI_RAD;
return 6371.01 * acos(sin(phi1) * sin(phi2) + cos(phi1) * cos(phi2) * cos(lam2 - lam1));}
Try This below method code to get the distance in meter between two location, hope it will help for you
public static double distance(LatLng start, LatLng end){
try {
Location location1 = new Location("locationA");
location1.setLatitude(start.latitude);
location1.setLongitude(start.longitude);
Location location2 = new Location("locationB");
location2.setLatitude(end.latitude);
location2.setLongitude(end.longitude);
double distance = location1.distanceTo(location2);
return distance;
} catch (Exception e) {
e.printStackTrace();
}
return 0;
}
Why are you writing the code for calculating the distance by yourself?
Check the api's in Location class
You should use Haversine Distance Formulas
Haversine Formulas used to calculate the great distance between two points on the earth.
public void haversine(double lat1, double lon1, double lat2, double lon2) {
double Rad = 6372.8; //Earth's Radius In kilometers
// TODO Auto-generated method stub
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.sin(dLon / 2) * Math.sin(dLon / 2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
haverdistanceKM = Rad * c;
}
Updated on October 8, 2021
Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.
Java
public float getDistance(double startLat,double startLang,double endLat,double endLang) {
Location locStart = new Location("");
locStart.setLatitude(startLat);
locStart.setLongitude(startLang);
Location locEnd = new Location("");
locEnd.setLatitude(endLat);
locEnd.setLongitude(endLang);
return locStart.distanceTo(locEnd);
}
Kotlin
private fun getDistance(
startLat: Double,
startLang: Double,
endLat: Double,
endLang: Double
): Float {
val locStart = Location("")
locStart.latitude = startLat
locStart.longitude = startLang
val locEnd = Location("")
locEnd.latitude = endLat
locEnd.longitude = endLang
return locStart.distanceTo(locEnd)
}
Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.
public float getMesureLatLang(double lat,double lang) {
Location loc1 = new Location("");
loc1.setLatitude(getLatitute());// current latitude
loc1.setLongitude(getLangitute());//current Longitude
Location loc2 = new Location("");
loc2.setLatitude(lat);
loc2.setLongitude(lang);
return loc1.distanceTo(loc2);
// return distance(getLatitute(),getLangitute(),lat,lang);
}