Android calculate distance between two latitude longitude points Code Answer
We use numerous applications that require and utilize the Google Maps. However, these maps are utilized to specify positions on Google Maps. We could use this feature to evaluate the interval between the two places. The easy way for mapping intervals depends on latest coming math. The formula that calculates the distance from latitude and longitude is called the Haversine formula. This formula utilizes the globular trigonometry to designate the distance between two points.
get distance from latitude and longitude code android
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);
}
Finally, the condition when the straight line is not sufficient, we use another technique to visualizing the distance between two points. In Google Maps driving direction are one of the most popular way for the distance calculating. Hope so you have caught you answer. Let us inform about your valuable suggestions in the comment box.