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);
}
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);
}
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);
}
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.
I'm new to Android development and Google Maps API, so I'd like to know how it'd be possible to calculate the distance between a marker and a Polilyne path.
To give you an idea, the application has to basically tell the user which Polilyne path (there's like 10) is closer to the marker set by the user on the map.
private double distance(double lat1, double lon1, double lat2, double lon2, char 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 can be used to measure distance between two points, can return metric or imperial units.
Just try to iterate the points of the polyline and the minimal distance between the polyline points and the closest point of the polyline and the marker will be the distance. (Or you can use the average)
There are many algorithms to find shortest distance from current location to a polyline or polygon. One you can find # https://stackoverflow.com/a/7803519
in my app i am trying to calculate the distance a person traveling from one place to the other. For that i am using the Haversine formula,
R = earth’s radius (mean radius = 6,371km)
Δlat = lat2− lat1
Δlong = long2− long1
a = sin²(Δlat/2) + cos(lat1).cos(lat2).sin²(Δlong/2)
c = 2.atan2(√a, √(1−a))
d = R.c
getting the latitude and longitude of starting place and reaching place i am calculating the distance in kms. But others say that this distance calculation works only if travelled by airways and get varies if user travels by roadways.
If it is so how can i get a correct distance while traveling through road ways.
please help me friends
This method is in standard API (Location.distanceBetween method))
http://developer.android.com/reference/android/location/Location.html
Use the following formula to find the distance between two lat-longs:
private double calculateDistance(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 == "M") {
dist = dist * 0.8684;
}
return (dist);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts decimal degrees to radians :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double deg2rad(double deg)
{
return (deg * Math.PI / 180.0);
}
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
/*:: This function converts radians to decimal degrees :*/
/*:::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::::*/
private double rad2deg(double rad)
{
return (rad * 180.0 / Math.PI);
}
Pass lat,longs with the function and unit in which you want distance ("K" for kilometer and "M" for Miles).
Hope this will help you.
Location.distanceBetween() will do this for two Latitude Longitude coordinate points.
When you travel by road (Even via air-travel too), bearing of the Earth comes into play since the Earth is not flat. but this Haversine formula should already take care of it. Secondly, what people might have been saying, is that, when you travel via road, you don't go just straight between two points. you take quite a number of turns. And to "precisely" tell the distance between two points, you might want to take those turns into consideration too.
i found the sample code in the following link to calculate the distance going through roads.
http://code.google.com/p/krvarma-android-samples/source/browse/#svn/trunk/GPSSample%253Fstate%253Dclosed
Its seems distance trough radways bigger, because trains and trucks moves not along straight line but airplances do.
You need road map to occurate compute distance