we develop a application that we need calculate road distance with two Geographical spots (lat & long) like google API and i cant use google API. please help us..thanks
You can calculate distance in kilometers by using following code:
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
private double rad2deg(double rad)
{
return (rad * 180.0 / Math.PI);
}
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);
}
Related
I am developing an android app in which user can know distance between him and other users. I am using tomtom location api for this. I just want to know how to get the distance between current user and other user while i have latitude and longitude of both of them ?
Thanks in advance
I assume that you just need to calculate the great-circle distance between two points.
There is math explenation hidden under this: http://www.movable-type.co.uk/scripts/latlong.html
Check TomTom class: com.tomtom.online.sdk.common.util.DistanceCalculator
and methods:
DistanceCalculator#greatCircleDistance(LatLng latLng, LatLng refLatLng)
DistanceCalculator#calcDistInKilometers(LatLng destination, LatLng origin)
If you need to calculate precisely route distance, use routing sdk: https://developer.tomtom.com/maps-android-sdk/routing
If you already have coordinates of both users then you can calculate using below function. Answer is from SO thread here.
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);
}
By using fused API
I'm getting speed as 3.0 something. But I am in the same place and am not traveling anywhere, so from where is the speed coming from? I am testing with a Samsung device and a Motorola device. The problem is appearing on the Motorola device.
I'm setting the interval with: mLocationRequest.setInterval(1000 * 60); But the problem shows up on the Motorola device before 60 seconds which is updating (approximately at 30 second)
Calculate like this,
Find distance between two points;
private double distanceCalc(double lat1, double lon1, double lat2, double lon2, char unit) {
if (lat1 == lat2 && lon1 == lon2) {
return 0.0;
} else {
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);
}
}
calculating as per the unit of kilometer.
private double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
This function converts decimal degrees to radians.
private double rad2deg(double rad) {
return (rad * 180.0 / Math.PI);
}
This function converts radians to decimal degrees.
To get speed,Calculate time difference between both and convert in to hours and speed as distance/time.Here i have done with km/h.
Till now, I have inserted all the sets of latitude and longitude in my database after every 5 sec from the path I have travelled.
Now after reaching the destination I have to calculate the distance from the sets of Latitude, longitude. It's not about calculating the travelling distance between two points but to get the total distance travelled from set of lat,long in my database .
I have thought a way of calculating the total distance by getting the distance between the two points
and add them up at last
Is there any better solution than this? please suggest and help
Already read this SO post but not getting how I will get the total distance by using the Google Maps Distance Matrix API
///////service to insert coordinates into the database.
private class MyReceiver extends BroadcastReceiver {
#Override
public void onReceive(Context arg0, Intent arg1) {
String name = arg1.getAction();
if (name.equalsIgnoreCase("Location_Changed")) {
db.insertlatlong(new Latilongi(arg1.getStringExtra("latitude"),
arg1.getStringExtra("longitude"),currentDateandTime));
} else if (arg1.getAction().matches("android.location.PROVIDERS_CHANGED")) {
Toast.makeText(MapActivity.this, "in android.location.PROVIDERS_CHANGED",
Toast.LENGTH_SHORT).show();
}
}
}
you can use the following method that will give you acurate result
public double CalculationByDistance(LatLng StartP, LatLng EndP) {
int Radius = 6371;// radius of earth in Km
double lat1 = StartP.latitude;
double lat2 = EndP.latitude;
double lon1 = StartP.longitude;
double lon2 = EndP.longitude;
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));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value", "" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
}
try this one
private double distance(double lat1, double lon1, double lat2, double lon2, String unit) {
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);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private static double deg2rad(double deg) {
return (deg * Math.PI / 180.0);
}
You can insert only distance between two co-ordinates and lastly sum up all distances you had in database. Distance achieve like below.
float distanceInMeters = lastLocation.distanceTo(myLocation);
where "lastLocation" and "myLocation" are of Location object.
If you dont have Location object than you can try this.
Location lastLocation = new Location("");
lastLocation.setLatitude(latitude);
lastLocation.setLongitude(longitude);
Location myLocation = new Location("");
myLocation.setLatitude(otherlatitude);
myLocation.setLongitude(otherlongitude);
float distanceInMeters = lastLocation.distanceTo(myLocation);
Ok guys, i'm dealing with this situation in my Android App. I have:
Two points (origin and destination).
A set of routes or paths in which can pass a car.
So.. my question is: how can I know what of these routes is the most nearest route from my location (origin) that can take me to another location (destination)?
I neeed to use Google Maps API? What part of the API?
Can you help me?
If you are interested you can compute the direction between two points. It will not however consider the actual roads but rather the air distance. But I don't think that they are very off. So to sum it up, eite use the google API as suggested or you can use something like the below.
public double distFrom(double lat1, double lng1, double lat2, double lng2) {
{
double theta = lng1 - lng2;
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 / Math.PI);
}
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);
}