这更像是一个数据结构问题。我正从我的Android设备中检索经度和纬度。每个位置都有与之关联的状态。是的,对于警告,可以是假的。如果设备没有互联网连接,我将它们添加到队列中。重新建立连接后,队列中的值将全部推送到Web服务器。现在显然服务器连接很慢,所以我不想在我的队列中添加重复项,如果我不必这样做,我会在将其添加到队列之前检查是否记录了该位置。我这样做是通过制作一个以纬度为关键字的hashmap和一个像这个LocationData(double lat,double lng,boolean warning)这样的Location Object作为值。
这是我意识到我搞砸了的地方。 Latitude不保证是唯一的,因此如果我将它用作键,则必须返回该值的另一个数据结构(我正在考虑LocationData的arraylist)。唯一的另一个复杂因素是,如果确定某个位置确实存在且状态为true而另一个位于同一位置并且状态为false,那么我只需将状态更改为false即可记录新值(警告>确定)。有一个更好的方法吗?对于这样的任务,我的方法似乎过于膨胀。
我的位置类,用于跟踪所有已记录的位置
// Returns true if the location was already in the hashmap and doesn't need changed, otherwise
// updates/adds and returns false
protected boolean logLocation(String lat, double lng, boolean status){
ArrayList<LocationData> latitudeArray = recents.get(lat);
double latitude = Double.parseDouble(lat);
LocationData locTemp = new LocationData(latitude, lng, status);
if(latitudeArray != null) {
int indexCheck = latitudeArray.indexOf(locTemp);
if(indexCheck != -1){
LocationData holder = latitudeArray.get(indexCheck);
if((holder.getWarning() == true) && (locTemp.getWarning() == false)){
holder.setWarning();
return false;
}else{
return true;
}
}else{
latitudeArray.add(locTemp);
return false;
}
}else{
ArrayList<LocationData> newLocArray = new ArrayList();
newLocArray.add(locTemp);
recents.put(lat, newLocArray);
return false;
}
}
LocationData类
public class LocationData implements Comparable{
private double latitude;
private double longitude;
private boolean warning;
public LocationData(double lat, double lng, boolean warn){
this.latitude = lat;
this.longitude = lng;
this.warning = warn;
}
public double getLatitude(){
return this.latitude;
}
public double getLongitude(){
return this.longitude;
}
public boolean getWarning(){
return this.warning;
}
public void setWarning(){
this.warning = false;
}
@Override
public int compareTo(Object obj) {
LocationData compare = (LocationData) obj;
boolean equal = (latitude == compare.getLatitude()) && (longitude == compare.getLongitude());
if(equal){
return 0;
}else{
return -1;
}
}
不是很重要,但这是使用代码
的代码段public static void handleWarnings(Context context, boolean flag) {
OfflineQueue queue = OfflineQueue.getInstance();
LocationSingleton single = LocationSingleton.getInstance();
Location location = single.getLocation();
ArrayList queueList = queue.determineQueue(flag);
if (location != null) {
double lat = location.getLatitude();
double lng = location.getLongitude();
if (!StaticChecks.networkCheck(context)) {
if (!single.logLocation(lat + "", lng, flag)) {
queueList.add(location);
Toast.makeText(context, "Location Logged and Added to Queue", Toast.LENGTH_SHORT).show();
}else{
Toast.makeText(context, "Location Already Logged", Toast.LENGTH_SHORT).show();
}
} else {
if (!single.logLocation(lat + "", lng, flag)) {
Toast.makeText(context, "Longitude: " + lng + "\nLatitude: " + lat, Toast.LENGTH_SHORT).show();
} else {
Toast.makeText(context, "Location Already Logged", Toast.LENGTH_SHORT).show();
}
}
}else{
Log.e("STATIC", "There is no current Location Data in Update");
Toast.makeText(context, "There is no current Location Data ....", Toast.LENGTH_SHORT).show();
}
}