java - How to show the distance in kilometer on activity using google map in Android -
i trying distance of nearby event location (from google map) , show on activity. not sure button need use show distance , java code show distance in kilometer on activity?
if know current latlong or targeted location lat long can find distance using following method.
public double finddistance(latlng fromlatlan, latlng tolatlan) { int earthradious = 6371;// radius of earth in km double latfrom = fromlatlan.latitude; double latto = tolatlan.latitude; double lonfrom = fromlatlan.longitude; double lonto = tolatlan.longitude; double difflat = math.toradians(latto - latfrom); double difflon = math.toradians(lonto - lonfrom); double avalue = math.sin(difflat / 2) * math.sin(difflat / 2) + math.cos(math.toradians(latfrom)) * math.cos(math.toradians(latto)) * math.sin(difflon / 2) * math.sin(difflon / 2); double c = 2 * math.asin(math.sqrt(avalue)); double results = earthradious * c; double kms = results / 1; decimalformat newformat = new decimalformat("####"); int killometer = integer.valueof(newformat.format(kms)); double meter = results % 1000; int metersdistance = integer.valueof(newformat.format(meter)); log.i("value", "" + results + " killometer " + killometer + " meter " + metersdistance); return earthradious * c; }
Comments
Post a Comment