How to calculate distance between two coordinates in google map? - android

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

Related

How to sort ArrayList of LatLng

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.

How can I calculate a current distance to polyline points on google maps v2 in android?

I have used tutorial from the link below to display Google map route in Android app. My question is how can I calculate distance to polyline points on map? Like when I use Google maps app and it tells when a street turn is getting close. I want to implement similar feature in my app. I am able to display the route polyline on the map and it updates itself while I drive along it but I want it to warn me 500 feet in advance of a coming turn. How can I do that?
Here is the link:
http://jigarlikes.wordpress.com/2013/04/26/driving-distance-and-travel-time-duration-between-two-locations-in-google-map-android-api-v2/
I use this method for Markers. Assuming you have Latitude and Longitude of the points that make up the Polyline this should do:
public class MapUtils {
public static float distBetween(LatLng pos1, LatLng pos2) {
return distBetween(pos1.latitude, pos1.longitude, pos2.latitude,
pos2.longitude);
}
/** distance in meters **/
public static float distBetween(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 (float) (dist * meterConversion);
}
}
To determine wether the road is turning, I would look into euclidean angle between vectors (x being current location and y being a polyline point)
Simply take your current location and a LatLng from some distance ahead for this.
Calculation is based on: http://en.wikipedia.org/wiki/Euclidean_space#Angle
Location currentLocation; // obtained somewhere in your code
LatLng polylinePoint; // a point further ahead
double cLat = currentLocation.getLatitude();
double cLon = currentLocation.getLongitude();
double pLat = polylinePoint.latitude;
double pLon = polylinePoint.longitude;
double angle = Math.acos(
(cLat*pLat+cLon+pLon) / norm(cLat,cLon)*norm(pLat,cLon));
private double norm(double x, double y) {
return Math.sqrt(Math.pow(x, 2)*Math.pow(y, 2));
}
This is untested so might contain error.

get distance between Current location and Selected Place in android

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

how to find the distance between two geopoints?

double distance;
Location locationA = new Location("point A");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
the above code is not working and i am getting 0.0 Km as distance?
Also in the constructor of the location class, what does the string provider mean.
In the above code i am using PointA and PointB as the providers.
why is the above code not working?
thank you in advance.
LOGCAT
05-09 17:48:56.144: INFO/current loc(1573): lat 0.0 lng 0.0
05-09 17:48:56.155: INFO/checklocation loc(1573): lat 54.4288665 lng 10.169366
Just a quick snippet since I didn't see a complete and simple solution with GeoPoints above:
public float getDistanceInMiles(GeoPoint p1, GeoPoint p2) {
double lat1 = ((double)p1.getLatitudeE6()) / 1e6;
double lng1 = ((double)p1.getLongitudeE6()) / 1e6;
double lat2 = ((double)p2.getLatitudeE6()) / 1e6;
double lng2 = ((double)p2.getLongitudeE6()) / 1e6;
float [] dist = new float[1];
Location.distanceBetween(lat1, lng1, lat2, lng2, dist);
return dist[0] * 0.000621371192f;
}
If you want meters, just return dist[0] directly.
Try Location.distanceBetween(..)
Update:
If you are getting lat/lon from GeoPoint then they are in microdegrees. You must multiply by 1e6.
As stated above, the Location Class is the way to go. Here is the code I have used :
Location locationA = new Location("point A");
locationA.setLatitude(pointA.getLatitudeE6() / 1E6);
locationA.setLongitude(pointA.getLongitudeE6() / 1E6);
Location locationB = new Location("point B");
locationB.setLatitude(pointB.getLatitudeE6() / 1E6);
locationB.setLongitude(pointB.getLongitudeE6() / 1E6);
double distance = locationA.distanceTo(locationB);
In this example, both pointA and pointB are instances of the GeoPoint class.
This is not an answer, but note the signature for the Location constructor is Location(String provider).
i.e. the String you pass to the constructor should be one of LocationManager.GPS_PROVIDER, LocationManager.NETWORK_PROVIDER or LocationManager.PASSIVE_PROVIDER, not "point A" and "point B".
An alternative way to accomplish the above,
public class Distance {
public static 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;
//if (unit == "K") {
// dist = dist * 1.609344;
// else if (unit == "N") {
//dist = dist * 0.8684;
//}
return (dist);
}
public static final double PI = 3.14159265;
public static final double deg2radians = PI/180.0;
public static double getDistance(double latitude1, double longitude1, double latitude2,double longitude2) {
double lat1 = latitude1 * deg2radians;
double lat2 = latitude2 * deg2radians;
double lon1 = longitude1 * deg2radians;
double lon2 = longitude2 * deg2radians;
// Williams gives two formulae;
// this is the more accurate for close distances.
// In practice, the two differed only in the 8th or 9th place, for
// separations as small as 1 degree.
double radd = 2 * Math.asin(Math.sqrt(Math.pow(Math.sin((lat1 - lat2) / 2),
2.0)
+ Math.cos(lat1)
* Math.cos(lat2)
* Math.pow(Math.sin((lon1 - lon2) / 2), 2.0)));
return radd;
}
/* ::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::: */
/* :: 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.0 / Math.PI);
}
}
double CalculateDistance( double nLat1, double nLon1, double nLat2, double nLon2 )
{
double nRadius = 6371; // Earth's radius in Kilometers
// Get the difference between our two points
// then convert the difference into radians
double nDLat = ToRad(nLat2 - nLat1);
double nDLon = ToRad(nLon2 - nLon1);
// Here is the new line
nLat1 = ToRad(nLat1);
nLat2 = ToRad(nLat2);
double nA = pow ( sin(nDLat/2), 2 ) + cos(nLat1) * cos(nLat2) * pow ( sin(nDLon/2), 2 );
double nC = 2 * atan2( sqrt(nA), sqrt( 1 - nA ));
double nD = nRadius * nC;
return nD; // Return our calculated distance
}
http://www.jaimerios.com/?p=39

Distance between two locations isn't right

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

Categories

Resources