>我之前有一个问题,我想知道如何根据纬度和经度找到两个位置之间的距离。我链接到一个答案,现在我正在尝试实现 SortPlaces.java但它无法解析符号 Place。IDE 也没有为我提供导入类的选项。这是否意味着我必须自己创建 Place 类?如果是这样,那会是什么?还是 Place 类已经存在,而我无法导入它,因为我没有合适的库? 谢谢,这是代码。
import com.google.android.gms.maps.model.LatLng;
public class SortPlaces implements Comparator<Place> {
LatLng currentLoc;
public SortPlaces(LatLng current){
currentLoc = current;
}
@Override
public int compare(final Place place1, final Place place2) {
double lat1 = place1.latlng.latitude;
double lon1 = place1.latlng.longitude;
double lat2 = place2.latlng.latitude;
double lon2 = place2.latlng.longitude;
double distanceToPlace1 = distance(currentLoc.latitude, currentLoc.longitude, lat1, lon1);
double distanceToPlace2 = distance(currentLoc.latitude, currentLoc.longitude, lat2, lon2);
return (int) (distanceToPlace1 - distanceToPlace2);
}
public double distance(double fromLat, double fromLon, double toLat, double toLon) {
double radius = 6378137; // approximate Earth radius, *in meters*
double deltaLat = toLat - fromLat;
double deltaLon = toLon - fromLon;
double angle = 2 * Math.asin( Math.sqrt(
Math.pow(Math.sin(deltaLat/2), 2) +
Math.cos(fromLat) * Math.cos(toLat) *
Math.pow(Math.sin(deltaLon/2), 2) ) );
return radius * angle;
}
}
查看您的另一个问题,您需要在排序代码中将 Place 类替换为您的露营地类:
public class SortCampgrounds implements Comparator<Campsite> {
LatLng currentLoc;
public SortCampgrounds(LatLng current){
currentLoc = current;
}
@Override
public int compare(final Campsite campsite1, final Campsite campsite2) {
double lat1 = campsite1.getLatitude();
double lon1 = campsite1.getLongitude();
double lat2 = campsite2.getLatitude();
double lon2 = campsite2.getLongitude();
double distanceToCampsite1 = distance(currentLoc.latitude, currentLoc.longitude, lat1, lon1);
double distanceToCampsite2 = distance(currentLoc.latitude, currentLoc.longitude, lat2, lon2);
return (int) (distanceToCampsite1 - distanceToCampsite2);
}
public double distance(double fromLat, double fromLon, double toLat, double toLon) {
double radius = 6378137; // approximate Earth radius, *in meters*
double deltaLat = toLat - fromLat;
double deltaLon = toLon - fromLon;
double angle = 2 * Math.asin( Math.sqrt(
Math.pow(Math.sin(deltaLat/2), 2) +
Math.cos(fromLat) * Math.cos(toLat) *
Math.pow(Math.sin(deltaLon/2), 2) ) );
return radius * angle;
}
}
然后使用自定义比较器按到当前位置的距离对露营地进行排序,并获得最近的:
public void onMapReady(GoogleMap googleMap) {
mMap = googleMap;
// GET ALL CAMPSITES FROM DATABASE
ArrayList<Campsite> campsites = db.getAllCampsites();
// LOOP THROUGH EACH CAMPSITE AND ADD A MARKER FOR EACH CAMPSITE
for (Campsite campsite : campsites) {
LatLng latLng = new LatLng(campsite.getLatitude(), campsite.getLongitude());
mMap.addMarker(new MarkerOptions().position(latLng).title(campsite.getName()));
}
// PLACE MARKER FOR CURRENT LOCATION (STATIC LOCATION)
LatLng currentLocation = new LatLng(34.1691, -118.4167);
mMap.addMarker(new MarkerOptions().position(currentLocation).title("Your location"));
// Add this to sort and get the closest Campsite:
Collections.sort(campsites, new SortCampgrounds(currentLocation));
// Get the first one from the list, this will be the closest:
Campsite closestCampsite = campsites.get(0);
}