How to find the distance between two locations on android ecillipse project &php .the project is based on online good transport system .The fare of the carrier needed to be found out so there is a need of finding distance between source & destination .Harversine formula was implemented for finding the shortest vehicle in the region
float[] results = new float[1];
Location.distanceBetween(source.latitude, source.longitude,
destination.latitude, destination.longitude,
results);
Computes the approximate distance in meters between two
locations, and optionally the initial and final bearings of the
shortest path between them. Distance and bearing are defined using the
WGS84 ellipsoid. The computed distance is stored in results[0]. If results has length
2 or greater, the initial bearing is stored in results[1]. If results has
length 3 or greater, the final bearing is stored in results[2].
#param startLatitude the starting latitude
#param startLongitude the starting longitude
#param endLatitude the ending latitude
#param endLongitude the ending longitude
#param results an array of floats to hold the results
#throws IllegalArgumentException if results is null or has length < 1
Try this code
double _distance = Location.distanceBetween(
_firstLatitude,
_firstLongitude,
_secondLatitude,
_secondLongitude,
_results);
Related
Does anyone know where the separate latitude and longitude distances can be found between two google markers? I can get the total distance using the haversine method such as this:
Find distance between two points on map using Google Map API V2
How can I get each value please?
Thanks
Frank
The Marker class has a Position property, so
double latitudeDifferenceInDegrees = marker1.getPosition().latitude - marker2.getPosition().latitude;
double longitudeDifferenceInDegrees = marker1.getPosition().longitude - marker2.getPosition().longitude;
You can convert the latitude difference into a distance in meters - each degree is equal to 110.574 kilometers:
double latitudeDifferenceInMeters = latitudeDifferenceInDegrees * 110574;
You can't do the same for longitude. On the equator, a longitude degree is about 111 km, but near the poles, it is much less. If you are only using this for short distances, I'd go for
double longitudeDifferenceInMeters = longitudeDifferenceInDegrees * 111320 * Math.cos((marker1.getPosition().latitude + marker2.getPosition().latitude) / 2);
Note that both can give negative values if the first marker lies south or west of the second marker. If you're interested in positive values only, apply Math.abs().
I am developing an application that uses google map to navigate the user to a given location. I want to orient the map so the destination point always be at the top of the map and the current location marker to be at the bottom and both markers to be vertically aligned and centered in the map (e.g the vertical line between them to be perpendicular to the screen)
My approach is to find the mid point of the markers and than to calculate the bearing to rotate the map so both markers end up vertically aligned and centered. Centering the mid point will center the markers but I can't calculate the correct value for the bearing.
Any help will be appreciated.
EDIT:
I've tried Location.bearingTo and Location.distanceBetween. For the same input they return different values and the one returned from Location.distanceBetween is what i am looking for.
EDIT2 (Code example):
public static void positionMap(GoogleMap map, LatLng start, LatLng end) {
// zoom the map so both locations are visible
LatLngBounds bounds = LatLngBounds.builder().include(start).include(end).build();
map.moveCamera(CameraUpdateFactory.newLatLngBounds(bounds, 200));
// find the bearing
float[] results = new float[3];
Location.distanceBetween(
start.latitude,
start.longitude,
end.latitude,
end.longitude,
results);
float bearing = results[2];
// position the map so the two markers are vertically aligned
CameraPosition position = map.getCameraPosition();
CameraPosition cameraPosition = new CameraPosition.Builder(position)
.target(Utils.median(start, end))
.bearing(bearing)
.build();
map.moveCamera(CameraUpdateFactory.newCameraPosition(cameraPosition));
}
Take a look the the distanceBetween method of a location object.
Here is the doc. The initial bearing is the bearing you need to use from the starting point, the final bearing is the bearing you will be on when you reach the destination. I think you would be interested in the initial bearing.
public static void distanceBetween (double startLatitude, double startLongitude, double endLatitude, double endLongitude, float[] results)
Added in API level 1
Computes the approximate distance in meters between two locations, and optionally the initial and final bearings of the shortest path between them. Distance and bearing are defined using the WGS84 ellipsoid.
The computed distance is stored in results[0]. If results has length 2 or greater, the initial bearing is stored in results[1]. If results has length 3 or greater, the final bearing is stored in results[2].
Parameters
startLatitude the starting latitude
startLongitude the starting longitude
endLatitude the ending latitude
endLongitude the ending longitude
results an array of floats to hold the results
Throws
IllegalArgumentException if results is null or has length < 1
You could try using Location.bearingTo().
Unfortunately this requires converting from LatLng to Location, but that should be simple enough.
first what I want to achive, than I think I can do it
I want to find streets on the edge of area where center point is point where I am standing.
Now for example I'm selecting area with 2km radius. I want to find 360 points (1 point for each degree) and check if point with this coordinates is street or not. Now 3 questions
How to get each point in distance from me
How to get latitude and longitude of this point
How to check if this point is street or not
How to get each point in distance from me
Having the Lat and Lng of source and destination points, you can use Location.distanceBetween method.
float[] results = new float[3];
Location.distanceBetween(srcLat, srcLng, destLat, destLng, results);
The distance between the two points is in index 0 : results[0]
How to get latitude and longitude of this point
Here is a link
How to check if this point is street or not
Here is another link.
Hope this help.
I am very new to Android and learning through it. So i want to create a arrow like pointer to show where is the fixed location is fixed, as the users move their device, the arrow should automatically updated(like compass).
I have googled for resources but i couldn't manage to find it. Anyone here knows how to do it? any examples or tutorials would be much appreciated.
Example:
fixed location: 20 latitude, 50 longitude
my location: 5 latitude 25, longitude
So how do i find the arrow pointed to fixed location?
If curLat and curLon are the current location's latitude and longitude and if refLat and refLon are the reference location latitude and longitude you may get the bearing from the current location to the reference location like this:
final float[] results= new float[3];
// The computed distance in meters is stored in results[0].
// If results has length 2 or greater, the initial bearing is stored in results[1].
// If results has length 3 or greater, the final bearing is stored in results[2].
Location.distanceBetween(curLat, curLon, refLat, refLong, results);
final float bearing = results[1];
If the distance grows result[2] differ more and more from results[1] unless the the course from the current location to the reference location follows a rhumb line (or loxodrome, see here: http://en.wikipedia.org/wiki/Rhumb_line).
hi friends in my gps application i did convert Geo point to pixels. i have two geo points so i convert this both of two points to pixels now i take different of this both pixels points and i want to convert this difference to kilometers
I wouldn't recommend using the view pixels to do distance calculation. If you have the geo points, you should use those. It all comes down to some geodetic calculation. And the accuracy depends on how you model the earth. What you want is to use geodetic great circle lines to perform distance calculations.
If you model the earth as a sphere (using law of cosines):
double earthAverageRadius = 6378137; //Average mean in meters when modeling the world as a sphere
double angle = Math.acos(Math.sin(point1.x) * Math.sin(point2.x)
+ Math.cos(point1.x) * Math.cos(point2.x) * Math.cos(point1.y- point2.y));
double distance = angle * pi * earthAverageRadius; // distance in metres
I would also recommend looking into the Haversine formula, which is numerically more stable. Using the haversine formula, the angle calculated in the previous code would be:
double a = Math.pow(Math.sin((point2.x-point1.x)/2.0), 2.0)
+ Math.cos(point1.x) * Math.cos(point2.x) * Math.pow(Math.sin((point2.y-point1.y)/2.0), 2.0);
double angle = 2 * Math.asin(Math.min(1.0, Math.sqrt(a)));
If you want increased accuracy (for large distances), you should consider modeling the earth as an ellipsoid, though the calculations for this are considerably harder.
EDIT: Also note that the above only holds if you give longitude and latitude in radians. So you'll have to make that conversion first too.