I have a function that return the distance between 2 locations
This is my function
public static float GetDistance(Location locationA, Location locationB ){
float distance = locationA.distanceTo(locationB);
return distance;
}
The float result what does it mean? air distance in kilometers?
I get 1.1 or 1.9 what does this result mean
Thanks
I think that can help you :
Calculating distance between two geographic locations
Returns the approximate distance in meters between this location and the given location. Distance is defined using the WGS84 ellipsoid.
Related
I am getting far and near LatLng as below :
mFarLeft = mMapView.getProjection().getVisibleRegion().farLeft;
mFarRight = mMapView.getProjection().getVisibleRegion().farRight;
mNearLeft = mMapView.getProjection().getVisibleRegion().nearLeft;
mNearRight = mMapView.getProjection().getVisibleRegion().nearRight;
Now, How can I calculate the distance between this far and near points?
Is there any method available to get distance in KM and Feet?
The fields farLeft, farRight, etc. are actually LatLng objects, according to the Android documentation. However, there is a field latLngBounds which represents the smallest bounding box including the visible region. This bounds object itself has two LatLng fields for the northeast and southwest corners of the box. Using the Haversine formula we can compute the distance between the corners of this box. For example, if you wanted to compute the height of the bounding box we can try:
public static double haversine(double lat1, double lon1, double lat2, double lon2) {
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
lat1 = Math.toRadians(lat1);
lat2 = Math.toRadians(lat2);
double a = Math.pow(Math.sin(dLat / 2),2) + Math.pow(Math.sin(dLon / 2),2) * Math.cos(lat1) * Math.cos(lat2);
double c = 2 * Math.asin(Math.sqrt(a));
return 6372.8 * c; // in kilometers
// if you want to return feet, then use 6372.8 * 3280.84 instead
}
public static void main(String[] args) {
LatLng ne = mMapView.getProjection().getVisibleRegion().latLngBounds.northeast;
LatLng sw = mMapView.getProjection().getVisibleRegion().latLngBounds.southwest;
double latLow = sw.latitude;
double latHigh = ne.latitude;
double longitude = sw.longitude;
// now compute the "height" of the bounding box
// note that the longitude value is the same
double height = haversine(latLow, longitude, latHigh, longitude);
}
Google provides a utility library for Maps that can, among other things,
Calculate distances, areas and headings via spherical geometry
Using the spherical geometry utilities in SphericalUtil, you can compute distances, areas, and headings based on latitudes and longitudes. Here are some of the methods available in the utility:
computeDistanceBetween() – Returns the distance, in meters, between two latitude/longitude coordinates.
computeHeading() – Returns the bearing, in degrees, between two latitude/longitude coordinates.
computeArea() – Returns the area, in square meters, of a closed path on the Earth.
interpolate() – Returns the latitude/longitude coordinates of a point that lies a given fraction of the distance between two given points. You can use this to animate a marker between two points, for example.
Refer to the reference documentation for a full list of methods in the utility.
The library is available on maven central.
dependencies {
compile 'com.google.maps.android:android-maps-utils:0.5+'
}
Source: https://developers.google.com/maps/documentation/android-api/utility/
I'm using SphericalUtil.computeOffset, which takes 3 params
public static LatLng computeOffset(LatLng from, double distance, double heading)
in this way:
LatLng position = new LatLng(50, 14);
double distance = 2 * 1000 * 1000 //2 thousand kilometers
LatLng result = SphericalUtil.computeOffset(position, distance, 90);
and I expect that resulting point will have same latitude, because I pass 90 degrees, which "strictly to the East" direction, but in real it returns point with
(46.76979930677399,40.79730389984821)
coordinates.
If i use less distance it will be closer to origin, if bigger - it becomes even worse
What am I doing wrong? Maybe I'm passing wrong angle?
I successfully retrieved the last location. I found many answers on SO about calculating the difference in distance between two points, but they seem to have their latitude and longitude separated?
My result is this(numbers changed for example):
Location[fused 12.345678,-123.456789 acc=30 et=+19h26m17s811ms]
How do I separate the latitude and longitude for use like this:
float[] results = new float[3];
Location.distanceBetween(startLatitude, startLongitude, endLatitude, endLongitude, results);
float distanceInMeters = results[0]
I found this code on this stack overflow question: distance between two locations is not right?
A Location has 2 methods to calculate the distance to another Location:
static void distanceBetween(double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)which writes the distance to the results array
float distanceTo(Location dest) which returns the distance as a float.
So you can either call distanceTo on your Location-object, or you can call Location.distanceBetween and pass in the 2 Latitude/Longitude pairs.
I'm trying to make a compass that points to a custom location in a Unity program. However it's completely off, it points in generally the wrong direction and I can't quite seem to figure out what is wrong with my logic.
I'm using the curved geometry bearing equation from here: http://www.yourhomenow.com/house/haversine.html
Get current location.
Calculate bearing from current location to target location.
Rotate screen compass relative to true north.
// Calculate bearing between current location and target location
// Get current GPS location
float lat1 = Input.location.lastData.latitude;
float lon1 = Input.location.lastData.longitude;
float lat2 = TargetLatitude;
float dLon = TargetLongitude - lon1;
// Calculate bearing
var y = Mathf.Sin(dLon) * Mathf.Cos(lat2);
var x = Mathf.Cos(lat1) * Mathf.Sin(lat2) -
Mathf.Sin(lat1) * Mathf.Cos(lat2) * Mathf.Cos(dLon);
var brng = ((Mathf.Atan2(y, x)*(180.0f/Mathf.PI)) + 360.0f) % 360.0f;
Dump.text = brng.ToString();
// Rotate the to target location relative to north. Z axis pointing out of screen.
transform.eulerAngles = new Vector3(0, 0, Mathf.MoveTowardsAngle(transform.localEulerAngles.z, Input.compass.trueHeading + brng, COMPASS_MAXDELTA));
If I remove the bearing offset in that code, it points north enough for a digital compass.
Make sure that all of your angles are in radians rather than degrees.
Using Mathf.Deg2Rad in cases such as Mathf.Sin(dLon * Mathf.Deg2Rad) should yield the correct result. The Haversine formula only works for angles in radians.
I am getting two GEO locations in my application and I have to calculate distance between them and after distance calculation I have to compare that result with the a defined threshold which is in my case 50 meters, how would I define that threshold in float. Also, Currently I my android fone is at the same location, but I always get the calculated distance between my 2 two geolocations determined after an interval to be more than 50 meters. How is it possible? I am taking threshold of 50 meters as:
private static final float DISTANCE_CHANGE_METERS = 50.0f; //50 meters
and I am calculating distance through following formula I found on StackOverflow:
public static float 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 Float(dist * meterConversion).floatValue();
}
Kindly help me out in this regard. Thanks!
Currently my android phone is at the same location, but I always get the calculated distance between my 2 two geolocations determined after an interval to be more than 50 meters. How is that possible?
It actually depends on how you are passing the latitude and longitude to the distFrom function. Just to remind you that there is a simpler way to calculate the distance.
You can use just distanceTo() function in android API using Location to calculate the distance between two places like this:
Location locationA = new Location("point A");
locationA.setLatitude(latA); // co-ordinates in double
locationA.setLongitude(lngA);
Location locationB = new Location("point B");
locationB.setLatitude(latB);
locationB.setLongitude(lngB);
float distance = locationA.distanceTo(locationB);
If after using this also you are getting a distance greater than 50 meters then your co-ordinates that you are providing are not that accurate. Its perfectly fine because its tough to get accuracy with less that 50 meter precision. It would be most accurate if you are using internet along with GPS. If you see here, even if you use ACCURACY_HIGH as a criteria to choose a Location Provider, it provides accuracy of less than 100 meters. It can be accurate upto 100, 70 or even 50 meters. So even the most accurate option is not guaranteed to give extreme accuracy. If you are using GPS/Network provider for getting Location, then it your situation is very much possible. Getting location using `Network Provider1 is also not that accurate as it can catch signals of 2 network towers on different occasions, which can be well apart physically.
Hope it helps and clears the point to some extent. Its the problem to accuracy of your Location Provider.
Could you just do:
if (distFrom(lat1,lng1,lat2,lng2) > DISTANCE_CHANGE_METERS)
{
//greater than threshhold
}