我有一个checkboxlist,用户可以选择一些路由,然后从服务器获取响应。我有方法gotoLocation
来更新标记的位置以及在地图中添加新标记
当一个新的插入到服务器端的表中时,使用相同的路由。
我使用HashMap,表格ID为键,标记为值,在我的情况下,密钥为unique
。
更新部分工作正常,但我遇到了添加部分的问题。因此,在用户选择了所需的路径后,它们将作为标记添加到地图中,并作为元素添加到HashMap中,但是当我插入具有相同路径和不同键的新路径时,会出现问题(id正在增加一个表)然后输入添加部分,并且正在识别新添加的部分的ID,但新标记未添加到地图上,而是添加到HashMap markerMap 。
此外,当新的Response再次到达方法时,我注意到最后添加的带有新id的标记不在HashMap中,并且当请求结束并且新添加的元素保持不变时它也只包含第一个请求的元素在HashMap中请求Life!这个行为是否有任何解释,我该如何解决?
代码:
public class Map extends FragmentActivity implements OnMapReadyCallback {
GoogleMap map;
HashMap<Integer, Marker> markerMap = new HashMap<Integer, Marker>();
Marker marker = null;
private void gotoLocation(int id, double lat, double lng,
String route_direct) {
// check = false;
final float zoom = 11;
LatLng ll = null;
if (markerMap.containsKey(id)) {
//Update block.
// Update the location.
// To figure out which ones was not updated.
idList.add(id);
marker = markerMap.get(id);
// Remove from the map fragment.
marker.remove();
// Remove from the HashMap tp add the new one.
markerMap.remove(id);
ll = new LatLng(lat, lng);
if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
MarkerOptions markerOpt = new MarkerOptions()
.title(route_direct).position(ll).visible(true);
marker = map.addMarker(markerOpt);
markerMap.put(id, marker);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll,
zoom);
map.moveCamera(update);
}
} else {
//Add block.
// Add the marker on the map in the case it does not exist in the
// HashMap before.
ll = new LatLng(lat, lng);
if (lat != 0 && lng != 0 && !route_direct.isEmpty()) {
MarkerOptions markerOpt = new MarkerOptions()
.title(route_direct).position(ll).visible(true);
marker = map.addMarker(markerOpt);
//Here when I add a new record to my table then it is being added to the HashMap but this block is being always entered since the new added one stays in the markerMap for the request life and is not as the added content of the first request.
markerMap.put(id, marker);
CameraUpdate update = CameraUpdateFactory.newLatLngZoom(ll,
zoom);
map.moveCamera(update);
}
}
}
修改
public class Map extends FragmentActivity {
GoogleMap map;
HashMap<Integer, String> markerMap = new HashMap<Integer, String>();
static String marker_string;
static Marker marker = null;
private void gotoLocation(int id, double lat, double lng,
String route_direct) {
final float zoom = 11;
LatLng ll = null;
if (markerMap.containsKey(id)) {
// Update the location.
marker_string = markerMap.get(id);
String[] marker_string_split = marker_string.split(",");
double latit = Double.parseDouble(marker_string_split[0]);
double longit = Double.parseDouble(marker_string_split[1]);
String rout_dirc = marker_string_split[2];
LatLng LL_2 = new LatLng(lat, lng);
MarkerOptions markerOpt2 = new MarkerOptions().title(route_direct)
.position(LL_2);
// This here doest work.
marker.remove();
// Remove from the HashMap tp add the new one.
markerMap.remove(id);
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll);
marker = map.addMarker(markerOpt);
String lat1 = Double.toString(lat);
String lng1 = Double.toString(lng);
String data = lat1 + "," + lng1 + "," + route_direct;
markerMap.put(id, data);
zoom();
} else {
// Add a new marker
String lat1 = Double.toString(lat);
String lng1 = Double.toString(lng);
String data = lat1 + "," + lng1 + "," + route_direct;
markerMap.put(id, data);
ll = new LatLng(lat, lng);
MarkerOptions markerOpt = new MarkerOptions().title(route_direct)
.position(ll);
marker = map.addMarker(markerOpt);
zoom();
}
}
}