Reputation: 1
I am trying to sort a list of latitude and longitude coordinates based on their distance from the user's current location. I've tried using comparators, but just can't seem to make it work. I've tried many different ways of implementing the comparator, including using an arraylist, and tried many different tutorials around the web. I was thinking about using a collection and then sorting it, but don't really know how. Am I on the right track with that? My code is below. If anyone could point me in the right direction, that would be great. Thanks so much!
package com.example.qdogg.mealmapnb;
//@Author Quinn Clark 2018
//
/**
*Sorting based on proximity soon
*/
import android.content.Intent;
import android.location.Location;
import android.os.Bundle;
import android.view.View;
import android.widget.AdapterView;
import android.widget.AdapterView.OnItemClickListener;
import android.widget.ListView;
import com.google.android.gms.maps.model.LatLng;
import com.google.android.gms.maps.model.Marker;
import java.util.ArrayList;
import java.util.Collections;
import java.util.Comparator;
import java.util.List;
public abstract class SleepMap extends MainActivity implements
OnItemClickListener, LocationProviderSleepMap.LocationProviderListener {
ListView listView;
/**
* Called when the activity is first created.
*/
@Override
public void onCreate(Bundle savedInstanceState) {
super.onCreate(savedInstanceState);
setContentView(R.layout.activity_sleep_map);
listView = (ListView) findViewById(R.id.sleeplist);
listView.setOnItemClickListener(this);
}
@Override
public void gotCurrentLocation(Location location) {
final Location CLSM = new Location("");
CLSM.setLatitude(location.getLatitude());
CLSM.setLongitude(location.getLongitude());
Location IFCSM = new Location("");
IFCSM.setLatitude(51.042580);
IFCSM.setLongitude(-114.062782);
Location BGCSM = new Location("");
BGCSM.setLatitude(51.039370);
BGCSM.setLongitude(-114.084020);
float CLSMtoIFCSM = CLSM.distanceTo(IFCSM);
float CLSMtoBGC = CLSM.distanceTo(BGCSM);
}
@Override
public void onItemClick(AdapterView<?> parent, View view,
int position, long id) {
switch (position) {
case 0:
Intent innfromthecold = new Intent(this, ifcactivity.class);
startActivity(innfromthecold);
break;
case 1:
Intent boysandgirlsclub = new Intent(this, bgcactivity.class);
startActivity(boysandgirlsclub);
break;
default:
//Nothing
}
}
}
Upvotes: 0
Views: 455
Reputation: 8758
In pseudo code it should look like this
public class DistanceComparator implements Comparator<Location> {
private Location current;
public DistanceComparator(Location loc) {
current = loc;
}
@Override
public int compare(Location l1, Location l2) {
int distance1 = calculateDistance(current, l1); // your code to calculate distance
int distance2 = calculateDistance(current, l2);
if (distance1 < distance2) return -1;
if (distance1 == distance2) return 0;
return 1;
}
}
Upvotes: 1