在我的项目中,将有两种类型的用户(导游,游客),其所有信息都将存储在Firebase中。并且每当有一位游客离开导游范围(例如1KM)时,该特定游客和导游就会收到通知。 如何将所有的旅游LatLng与导游LatLng进行比较,当超出范围时,两者都会收到通知?
这是我目前用于1位导游和1位游客之间比较距离的代码。
public double CalculationByDistance(LatLng tourist,LatLng tourguide) {
int Radius = 6371;// radius of earth in Km
double lat1 = tourist.latitude;
double lat2 = tourguide.latitude;
double lon1 = tourist.longitude;
double lon2 = tourguide.longitude;
double dLat = Math.toRadians(lat2 - lat1);
double dLon = Math.toRadians(lon2 - lon1);
double a = Math.sin(dLat / 2) * Math.sin(dLat / 2)
+ Math.cos(Math.toRadians(lat1))
* Math.cos(Math.toRadians(lat2)) * Math.sin(dLon / 2)
* Math.sin(dLon / 2);
double c = 2 * Math.asin(Math.sqrt(a));
double valueResult = Radius * c;
double km = valueResult / 1;
DecimalFormat newFormat = new DecimalFormat("####");
int kmInDec = Integer.valueOf(newFormat.format(km));
double meter = valueResult % 1000;
int meterInDec = Integer.valueOf(newFormat.format(meter));
Log.i("Radius Value","" + valueResult + " KM " + kmInDec
+ " Meter " + meterInDec);
return Radius * c;
}