Distance between two locations isn't right - android

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

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

Adding a marker along half the distance in Maps API Android

I am doing an Android project using Google Maps API that gets 2 different locations and adds a marker halfway along the route.
I found out that this can be done through Javascript and Node.js, but I want to do it entirely in Google Maps API.
How can I get the distance between 2 points and add a marker half way along the route?
Thanks in advance! :D
This is the code to find the distance:
public double CalculationByDistance(GeoPoint StartP, GeoPoint EndP) {
int Radius=6371;//radius of earth in Km
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));
double valueResult= Radius*c;
double km=valueResult/1;
DecimalFormat newFormat = new DecimalFormat("####");
kmInDec = Integer.valueOf(newFormat.format(km));
meter=valueResult%1000;
meterInDec= Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value",""+valueResult+" KM "+kmInDec+" Meter "+meterInDec);
return Radius * c;
}
I'd use
http://googlemaps.github.io/android-maps-utils/javadoc/com/google/maps/android/SphericalUtil.html#interpolate(LatLng, LatLng, double)
in the Google Maps Android util library: http://googlemaps.github.io/android-maps-utils/

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 display on map the distance between two geopoints?

I am developing an app where in user has to record location and then locate the car.When he selects locate car option the car that is parked should be displayed.I calculate the distance but distance always shows 0 metres..i dono why?can anyone tell how to show the the distance between person and the car parked on map... i want to show the parked car on the map
you saved the position of the parked car? you've got the actual position of the person? so, then you should be able to calculate the difference and get at least the distance (at least of the line in sight)...
If you have the position of USER and parked car...
then this should work
Location locationA = new Location("USER");
locationA.setLatitude(latA);
locationA.setLongitude(lngA);
Location locationB = new Location("Parked Car");
locationB.setLatitude(latB);
LocationB.setLongitude(lngB);
distance = locationA.distanceTo(locationB);
Use this function which calculates the real distance:
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;
int meterConversion = 1609;
return new Double(dist * meterConversion).doubleValue();
}
and use it like this:
double distance = 0;
distance=distFrom(latA,lngA,latB,lngB);
if you want to convert the double to integer use this:
int distance=0;
distance=(int) distFrom(latA,lngA,latB,lngB);

How to calculate distance between two coordinates in google map?

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

Categories

Resources