【发布时间】:2014-08-13 17:30:43
【问题描述】:
我想知道如何检查给定点是否在另一个固定点的一定公里半径范围内?
【问题讨论】:
标签: java android google-maps
我想知道如何检查给定点是否在另一个固定点的一定公里半径范围内?
【问题讨论】:
标签: java android google-maps
Location 类具有计算点之间距离的方法
第一个选项(使用distanceTo 方法):
// firstLocation and secondLocation are Location class instances
float distance = firstLocation.distanceTo(secondLocation); // distance in meters
if (distance < 5000) {
// distance between first and second location is less than 5km
}
第二个选项(使用静态distanceBetween 方法):
// distance is stored in result array at index 0
float[] result = new float[1];
Location.distanceBetween (startLat, startLng, endLat, endLng, result);
if (result[0] < 5000) {
// distance between first and second location is less than 5km
}
【讨论】:
你可以试试下面这个功能:
public static double getDistanceFromLatLngInKm(LatLng c1, LatLng c2) {
int R = 6371; // Radius of the earth in km
double lat1 = c1.latitude;
double lat2 = c2.latitude;
double lon1 = c1.longitude;
double lon2 = c2.longitude;
double dLat = deg2rad(lat2-lat1);
double dLon = deg2rad(lon2-lon1);
double a =
Math.sin(dLat/2) * Math.sin(dLat/2) +
Math.cos(deg2rad(lat1)) * Math.cos(deg2rad(lat2)) *
Math.sin(dLon/2) * Math.sin(dLon/2)
;
double c = 2 * Math.atan2(Math.sqrt(a), Math.sqrt(1-a));
double d = R * c; // Distance in km
return d;
}
public static double deg2rad(double deg) {
return deg * (Math.PI/180);
}
希望对你有所帮助..
【讨论】:
试试这个:-
public static Double methodName(double nLat, double nLon,
double nLat2, double nLon2) {
double nD;
Location locA = new Location("point A");
locA.setLatitude(nLat);
locA.setLongitude(nLon);
Location locB = new Location("point B");
locB.setLatitude(nLat2);
locB.setLongitude(nLon2);
nD = locA.distanceTo(locB);
return nD;
}
【讨论】: