I am new to android developing, I want to make an application like give me distance on MapView between my current location and selected place.
please give me some suggestion for this.
Get the distance between current location and a selected place using android.
public static double distFrom(
double lat1, double lng1, double lat2, double lng2)
{
double earthRadius = 3958.75;
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));
double dist = earthRadius * c;
return dist;
}
Only a few lines of code:
Location l1=new Location("One");
l1.setLatitude(location.getLatitude());
l1.setLongitude(location.getLongitude());
Location l2=new Location("Two");
l2.setLatitude(Double.parseDouble(frnd_lat));
l2.setLongitude(Double.parseDouble(frnd_longi));
float distance_bw_one_and_two=l1.distanceTo(l2);
Related
I have an ArrayList of LatLngs. Is it possible to sequence them in order of distance from a reference LatLng? I'm practicing a delivery application which maps markers on the google map where i need to connect them using polylines (Like travelling sales person)
Comparing two LatLng objects in google map v2 android (I'v gone through this link.)
Please help. Thanks in advance.
Use this to get the distance between two points :
public static float distFrom(float lat1, float lng1, float lat2, float lng2) {
double earthRadius = 3958.75;
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));
double dist = earthRadius * c;
int meterConversion = 1609;
return (float) (dist * meterConversion);
}
And put the results in a tree map.
I am developing an app where in user has to record location and then locate the car.When he selects locate car option the car that is parked should be displayed.I calculate the distance but distance always shows 0 metres..i dono why?can anyone tell how to show the the distance between person and the car parked on map... i want to show the parked car on the map
you saved the position of the parked car? you've got the actual position of the person? so, then you should be able to calculate the difference and get at least the distance (at least of the line in sight)...
If you have the position of USER and parked car...
then this should work
Location locationA = new Location("USER");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("Parked Car");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
Use this function which calculates the real distance:
public static double distFrom (double lat1, double lng1, double lat2, double lng2 )
{
double earthRadius = 3958.75;
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));
double dist = earthRadius * c;
int meterConversion = 1609;
return new Double(dist * meterConversion).doubleValue();
}
and use it like this:
double distance = 0;
distance=distFrom(latA,lngA,latB,lngB);
if you want to convert the double to integer use this:
int distance=0;
distance=(int) distFrom(latA,lngA,latB,lngB);
I had plotted two points in Google Map. I want to compute the distance between the two points in terms of road way. Any solution or working code?
Just use Android's inbuilt Location class, for getting distance between two locations.
Also you can try this,
Location locationA = new Location("location A");
locationA.setLatitude(locationA.getLatitudeE6() / 1E6);
locationA.setLongitude(locationA.getLongitudeE6() / 1E6);
Location locationB = new Location("location B");
locationB.setLatitude(locationB.getLatitudeE6() / 1E6);
locationB.setLongitude(locationB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);
You can use following method for calculating distance between two lat-long points.
public static float distFrom (float lat1, float lng1, float lat2, float lng2 )
{
double earthRadius = 3958.75;
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));
double dist = earthRadius * c;
int meterConversion = 1609;
return new Float(dist * meterConversion).floatValue();
}
You can do it through the APIs: there is a ComputeDistanceBetween function in the "geometry" library, "spherical" namespace. Reference: https://developers.google.com/maps/documentation/javascript/geometry
It is used this way:
var distance = google.maps.geometry.spherical.computeDistanceBetween(latLong, latLong);
I have calculated the distance between two places by using Latitude and longitude.Some times it showing wrong distance.I need to calculate exact distance in miles. Could you please help. what should i make changes in my code.
Here is my Code.
lat2 and lng2 are the current location geo points I have taken these by NETWORK PROVIDER/GPS.
lat1 and lng2 are the destination geo poin
double earthRadius = 3958.75;//earthRadius in miles
// double earthRadius = 6371;
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;//distance in miles
Thanks you..
Try like this.
float results[] = new float[3];
Location.distanceBetween(latitude_a, longitude_a, latitude_b,longitude_b, results);
distance = results[0];
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);
}
I have used the algorithm on http://www.movable-type.co.uk/scripts/latlong.html to find the distance between two points.
My two points are
long1 = 51.507467;
lat1 = -0.08776;
long2 = 51.508736;
lat2 = -0.08612;
According to Movable Type Script the answer is 0.1812km
My application gives the result (d) as 0.230km
Check Haversine formula: http://www.movable-type.co.uk/scripts/latlong.html
double R = 6371; // earth’s radius (mean radius = 6,371km)
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(long2-long1);
a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c;
Why to reinvent your own distance calculator, there is one built into the Location class.
Check out
distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them.
Your implementation is correct. The distance given those longitudes and latitudes should produce a distance of 0.230 km. The normal input for coordinates, however, is (latitude, longitude). Putting them in backwards (longitude, latitude) produces the incorrect distance of 0.1812 km.
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
double lat1 = StartP.getLatitudeE6()/1E6;
double lat2 = EndP.getLatitudeE6()/1E6;
double lon1 = StartP.getLongitudeE6()/1E6;
double lon2 = EndP.getLongitudeE6()/1E6;
double dLat = Math.toRadians(lat2-lat1);
double dLon = Math.toRadians(lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2);
double c = 2 * Math.asin(Math.sqrt(a));
return Radius * c;
}
Ally your concept was right.May be a little bit chang in this line double c = 2 * Math.asin(Math.sqrt(a));