How to get covered distance by transit - android

I want to create an app in which I want to show how much distance does the user has been traveled.
I have tried using float distance = locationA.distanceTo(locationB); but it just draws a simple straight line from start to end and then calculates the distance.
I want to calculate the traveled distance based on the traveled route. Is it possible to do so using any Google maps API?

you just need service to continuously observer that user change it's location then you can use these function to calculate the distance.
public double GetDistanceInKm(double lat1, double lon1, double lat2, double lon2)
{
final int R = 6371;
// Radius of the earth in km
double dLat = deg2rad(lat2 - lat1);
// deg2rad below
double dLon = deg2rad(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(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;
// Distance in km
return d;
}
private double deg2rad(double deg)
{
return deg * (Math.PI / 180);
}

Assuming you have a bunch of lat long points making up a route. You have to take the distance from the previous point to the next point for all points and keep a total of that. In other words you need a-lot of lat lon data in between the route or you are just going to get a straight line.

You can use google maps api to get the distance
use this link to understand how to use it.

Related

How to get all possible latitude and longitude based specific distance from location

I want to restrict map to specific distance as example 15 km from my current location to restrict user navigate to another locations.
I can calculate the distance between 2 points correctly based on the following function
public static double distFrom(double lat1, double lng1, double lat2, double lng2) {
double earthRadius = 6371.0; // earth radius in km
double dLat = Math.toRadians(lat2 - lat1);
double dLng = Math.toRadians(lng2 - lng1);
double sindLat = Math.sin(dLat / 2);
double sindLng = Math.sin(dLng / 2);
double a = Math.pow(sindLat, 2) + Math.pow(sindLng, 2)
* Math.cos(Math.toRadians(lat1)) * Math.cos(Math.toRadians(lat2));
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
return dist;
}
but cannot get possible latitude and longitude from my location in all sides based on specific distance because user can navigate in any side in map.
I tried this example but not give my accurate result.
Check this answer here:
https://stackoverflow.com/a/7478827/1397821
Now put all angles 0-360 in the equations mentioned in the above answer to get all the Lat Longs.
Hope this helps

Calculating distance travelled as the user moves

I need to calculate distance as the user moves in android. I have the starting point coordinates and can get the user's current location. How should I calculate distance.
I don't think I can call GoogleDistance Api as my application would be a real time application and calling this API would be extremely overhead or should calling GoogleDirectionsAPI would work fine.
Also I would like to test the distance travelled. How should I do it? Is there some way of sampling the data or do I really need to go out in the real world and actually travel and then test my application?
Edited: I want the distance travelled by the user and not the straight line distance between the points
I hope following will help:
In my app i started collecting lat longs after every 3fts (I set 3ft just for testing). I collected lat longs as follows:
Now I started calculating distance from point 1 to 2 and then 2 to 3 and then 3 to 4 and so on. After summing up I got total distance from 1 to 5.
I used following functions to calculate distance:
public double GetDistanceFromLatLonInKm(double lat1, double lon1, double lat2, double lon2)
{
final int R = 6371;
// Radius of the earth in km
double dLat = deg2rad(lat2 - lat1);
// deg2rad below
double dLon = deg2rad(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2) + Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(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;
// Distance in km
return d;
}
private double deg2rad(double deg)
{
return deg * (Math.PI / 180);
}
This method is calculating exact distance for me.
You can calculate the distance between two point using the following function
double distanceBetweenTwoPoint(double srcLat, double srcLng, double desLat, double desLng) {
double earthRadius = 3958.75;
double dLat = Math.toRadians(desLat - srcLat);
double dLng = Math.toRadians(desLng - srcLng);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(srcLat))
* Math.cos(Math.toRadians(desLat)) * Math.sin(dLng / 2)
* Math.sin(dLng / 2);
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1 - a));
double dist = earthRadius * c;
double meterConversion = 1609;
return (int) (dist * meterConversion);
}
For the second Part: To use mock or sample data you can use "Fake GPS" app from the play store, which will generate mock location in your device.
Happy Coding
There are basically three ways to check the traveled distance -
By using GPS, check the coordinate between two points using distanceTo of Location class.
Using Accelerometer , you can check the movement of device using Accelerometer.
Using Step detector - this is recently updated by Android.
Hope this helps you to achieve your goal :)
For the first question . You have origin coordinates and the present user location coordinates so what you can do is google api v3 library provides a method that takes origin LatLng and destination LatLng and gives the distance . Add api v3 jar to your android build path.
For the second one goto google maps and give the location details it is accurate..

rotate map according current walk direction android google maps

I have an android application with a map of google.
Can anybody tell me how we rotate the map according user current direction walking? I have read a lot of information but still a litle bit confused.
Thanks
You can calculate bearing by comparing two points. You said that your user will be walking so that shouldn't be too hard to get two points a distance apart from each other.
When you have the two points do some math like so
lon1 = degToRad(lon1);
lon2 = degToRad(lon2);
lat1 = degToRad(lat1);
lat2 = degToRad(lat2);
double a = Math.Sin(lon2 - lon1) * Math.Cos(lat2);
double b = Math.Cos(lat1) * Math.Sin(lat2) - Math.Sin(lat1) * Math.Cos(lat2) * Math.Cos(lon2 - lon1);
double c = radToDeg(Math.Atan2(a, b)); // c is our bearing //
These are our conversion functions
public static double degToRad(double deg){
return deg * Math.PI / 180.0;
}
public static double radToDeg(double rad){
rad = rad * (180.0 / Math.PI);
if (rad < 0) rad = 360.0 + rad;
return rad;
}
Once you have the bearing you can pass it to your map using the CameraUpdateFactory.
map.moveCamera(CameraUpdateFactory.newCameraPosition(new CameraPosition(LatLng, zoom, tilt, bearing)));

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.

how to calculate distance travelled

I am developing an application in which I need to calculate the distance travelled. I am facing a problem while calculating the distance.
Here is my code:
locationmanager.requestLocationUpdates(bestProvider,0,0,new Listener());
public class Listener implements LocationListener{
#Override
public void onLocationChanged(Location location) {
if(location!=null) {
if(loc1==null) {
loc1=new Location(location);
}
speed=location.getSpeed()*3.6;
loc2=new Location(location);
dist+=loc1.distanceTo(loc2);
loc1=loc2;
breaktv.setText(""+dist/1000);
My intention is calculate the total distance travelled by car or something. But distanceTo() method is not working well for me.
Technically it should work.
I have some suggestions :
dist must be float (if it isn't already)
dist += loc2.distanceTo(location), there is no need to create loc1
This is the code I've used
if(lastLoc != null)
{
ttf = (location.getTime() - lastLoc.getTime()) / 1000;
int R = 6371;
double lat1 = Math.PI / 180.0 *lastLoc.getLatitude();
double lon1 = Math.PI / 180.0 *lastLoc.getLongitude();
double lat2 = Math.PI / 180.0 *location.getLatitude();
double lon2 = Math.PI / 180.0 *location.getLongitude();
// double dLon = Math.PI / 180.0 * (location.getLongitude() - lastLoc.getLongitude());
double dLat = (lat2-lat1);
double dLon = (lon2-lon1);
double a = Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(lat1) * Math.cos(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;
totalDistance += d;
}
lastLoc = location;
Here is another Question on SO where I have posted 2 Answers one for the User Defined function to get the distance and other to User android.Location class method to calculate it.
Hope this will help you.
When You start your jurney take the CurrentLocation and store it may be using SharedPreferences,
Then I think you need to Continuosly keep updating the Distance Travelled,
You just use OnLocationChanged same way and Call the method above two mentioned from there to know the Current Distance Travelled.
EDIT:
here is a good example that may help you,Link To OpenSource Project

Categories

Resources