Java Android位置日志数据结构
这更像是一个数据结构问题。我正在从android设备检索经度和纬度。每个位置都有与其关联的状态。正确表示好,错误表示警告。如果设备没有internet连接,我会将它们添加到队列中。一旦重新建立连接,队列中的值将全部推送到web服务器。现在很明显,服务器连接速度很慢,因此如果不需要,我不想向队列中添加重复,因此在将位置添加到队列之前,我会检查是否已记录该位置。为此,我制作了一个hashmap,其中latitude作为键,LocationData(double lat,double lng,boolean warning)这样的位置对象作为值 这就是我意识到自己搞砸了的地方。Latitude不能保证是唯一的,因此如果我将其用作键,它必须为该值返回另一个数据结构(我正在考虑LocationData的arraylist)。唯一的其他复杂情况是,如果确定存在状态为true的位置,并且在同一位置记录了状态为false的另一个位置,则我只需将状态更改为false(警告>确定)即可记录新值。有更好的方法吗?对于这样的任务,我的方法似乎过于臃肿了 My Location类,用于跟踪所有记录的位置Java Android位置日志数据结构,java,android,performance,data-structures,android-location,Java,Android,Performance,Data Structures,Android Location,这更像是一个数据结构问题。我正在从android设备检索经度和纬度。每个位置都有与其关联的状态。正确表示好,错误表示警告。如果设备没有internet连接,我会将它们添加到队列中。一旦重新建立连接,队列中的值将全部推送到web服务器。现在很明显,服务器连接速度很慢,因此如果不需要,我不想向队列中添加重复,因此在将位置添加到队列之前,我会检查是否已记录该位置。为此,我制作了一个hashmap,其中latitude作为键,LocationData(double lat,double lng,bool
// 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;
}
}
不是很重要,但这是使用该代码的代码段
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();
}
}
如果代码中有任何错误,我深表歉意。我把这些放在一起是为了证明我所想到的方法的概念。我还没有创建测试用例。
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();
}
}