0% found this document useful (0 votes)
21 views2 pages

Nearest References:: Public Class

This document contains code to calculate the distance between two latitude-longitude coordinate pairs. It defines a static method that takes in four float parameters for latitude and longitude of two locations. The method uses basic trigonometry formulas involving earth's radius, latitude and longitude differences to calculate the great-circle distance between the points in meters, and then converts it to feet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
0% found this document useful (0 votes)
21 views2 pages

Nearest References:: Public Class

This document contains code to calculate the distance between two latitude-longitude coordinate pairs. It defines a static method that takes in four float parameters for latitude and longitude of two locations. The method uses basic trigonometry formulas involving earth's radius, latitude and longitude differences to calculate the great-circle distance between the points in meters, and then converts it to feet.
Copyright
© © All Rights Reserved
We take content rights seriously. If you suspect this is your content, claim it here.
Available Formats
Download as DOCX, PDF, TXT or read online on Scribd
You are on page 1/ 2

NEAREST REFERENCES:

public class getDistanceList


{
/**
*
* @param lat1 Latitude of the First Location
* @param lng1 Logitude of the First Location
* @param lat2 Latitude of the Second Location
* @param lng2 Longitude of the Second Location
* @return distance between two lat-lon in float format
*/

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


}

https://stackoverflow.com/questions/32434781/how-to-get-nearest-location-from-a-list-of-lat-long-
using-google-api

protected void findClosestResources(LatLng mCurrentLocation, GoogleMap map,Context context){


Double currentLatitude=mCurrentLocation.latitude;
Double currentLongitude=mCurrentLocation.longitude;
Double longitudeDifferenceCorrection=1/Math.cos(currentLatitude);
Location=Place.query(LOCATION_TABLE_NAME, M_COLUMNS, "MIN(("+M_COLUMNS[1]+" -
"+currentLatitude+") * ("+M_COLUMNS[1]+" - "+currentLatitude+
") + (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+
") * (("+M_COLUMNS[2]+" - "+currentLongitude+") * "+longitudeDifferenceCorrection+"))",
null,null
, null, null);
if (Location.moveToFirst()) {//select the first row in the cursor object
getInformationAbout(Location);
PlaceMakerOn(map);
Location.close();//close the connection with database
}

You might also like