Android 如何通过给定纬度和经度计算最近的位置
可能重复:Android 如何通过给定纬度和经度计算最近的位置,android,latitude-longitude,Android,Latitude Longitude,可能重复: 我有一些纬度和经度,我需要找到离我现在位置最近的纬度和经度位置 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
我有一些纬度和经度,我需要找到离我现在位置最近的纬度和经度位置
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();
}
试试这个
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();
}
当你说位置时,你是指地址吗?当你说位置时,你是指地址吗?如果我有30个纬度和30个经度,那么如何找到哪一个是最近的井,那么你需要在这里运行30次for循环,你的第一个纬度将是相同的,下一个纬度将是你的30个不同的纬度,现在找到距离并得到你想要的答案我不想用这些lat-long,而不是第一个lat-long,亲爱的,这就是我要说的,你的第一个lat-long将是你当前位置的lat-long,最后两个参数将是你拥有的30个不同的值。现在它将第一次计算当前位置和第一个lat lon的值,然后下一次计算当前位置和第二个lat long的值。明白了吗?是的,我明白了,谢谢,但是如何找到当前位置纬度经度如果我有30个纬度和30个经度,那么如何找到最近的井,那么你需要在这里运行30次for循环你的第一个纬度长度将是相同的,下一个纬度长度将是你的30个不同的纬度长度,现在找到距离并得到你想要的答案我不想用这些lat-long,而不是第一个lat-long,亲爱的,这就是我要说的,你的第一个lat-long将是你当前位置的lat-long,最后两个参数将是你拥有的30个不同的值。现在它将第一次计算当前位置和第一个lat lon的值,然后下一次计算当前位置和第二个lat long的值。明白了吗?是的,我明白了,谢谢,但是如何找到最近的地点呢