0

I had a previous question where I was wondering how I could find the distance between two locations based on latitude and longitude. I was linked to an answer and now I am trying to implement SortPlaces.java but it cannot resolve the symbol Place. The IDE also does not give me an option to import the class. Does this mean I have to create the Place class myself? If so what would that like or? Or does the Place class already exist and I can't import it because I don't have the proper library? Thanks, here is the code.

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;
}
}
Etra
  • 61
  • 1
  • 1
  • 13

1 Answers1

0

Looking at your other question, you will need to replace the Place class with your Campsite class in the sorting code:

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;
    }
}

Then use the custom comparator in order to sort the Campsites by distance to current location, and get the closest:

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

}
Daniel Nugent
  • 43,104
  • 15
  • 109
  • 137
  • Awesome it works!! Now I just have to actually get the device location instead of static data, but I am sure that is easy. Thanks! – Etra Oct 02 '19 at 00:06
  • For getting current location, see here: https://stackoverflow.com/a/44993694/4409409 – Daniel Nugent Oct 02 '19 at 01:58