0
我正在創建一個出租車預訂android應用程序。 在這裏我通過使用webservice在地圖上添加標記,但問題我能夠改變位置,例如從服務器舊標記更新標記後仍然存在。那麼我如何才能識別舊標記並將其刪除?如何更新標記和識別標記在Google地圖中的位置
public Map<String, Marker> cabsMarker = new HashMap<>();
public List<ServiceData> list = new ArrayList<>();
private GoogleMap mMap;
public class ServiceData {
public String name;
public LatLng location;
public ServiceData(LatLng location, String name) {
this.location = location;
this.name = name;
}
}
public void add_all_real_time_cabs() throws NullPointerException {
broadcastReceiver = new BroadcastReceiver() {
@Override
public void onReceive(Context context, Intent intent) {
if (intent.getAction().equals(GCMPushReceiverService.DATA_MESSAGE_STATUS_SUCCESS)) {
try {
String message = intent.getStringExtra(GCMPushReceiverService.JSON_ARRAY_RESPONSE);
JSONArray jarr = new JSONArray(message);
for (int i = 0; i < jarr.length(); i++) {
JSONObject jsonObject = jarr.getJSONObject(i);
All_UserMapDetail driver = new All_UserMapDetail();
if (!jsonObject.isNull(GCMPushReceiverService.FIRST_NAME_DRIVER_KEY)) {
driver.setName(jsonObject.getString(GCMPushReceiverService.FIRST_NAME_DRIVER_KEY));
Log.e("driver name", driver.getName());
}
if (!jsonObject.isNull(GCMPushReceiverService.DRIVER_PHONE_KEY)) {
driver.setPhone(jsonObject.getString(GCMPushReceiverService.DRIVER_PHONE_KEY));
}
if (!jsonObject.isNull(GCMPushReceiverService.LAT_DRIVER_KEY)) {
driver.setLat(Double.parseDouble(jsonObject.getString(GCMPushReceiverService.LAT_DRIVER_KEY)));
}
if (!jsonObject.isNull(GCMPushReceiverService.LON_DRIVER_KEY)) {
driver.setLon(Double.parseDouble(jsonObject.getString(GCMPushReceiverService.LON_DRIVER_KEY)));
Log.e("hgcghcghc", String.valueOf(driver.getLat()));
}
ServiceData data = new ServiceData(new LatLng(driver.getLat(), driver.getLon()), driver.getName());
list.add(data);
}
refreshMarkers();
} catch (Exception e) {
e.printStackTrace();
}
}
}
};
}
這裏是我的refreshMarkers方法。
public void refreshMarkers() {
Map<String, Marker> updatedMarkers = new HashMap<String, Marker>();
for (ServiceData data : list) {
// if marker exists move its location, if not add new marker
Marker marker = cabsMarker.get(data.name);
if (marker == null) {
marker = mMap.addMarker(new MarkerOptions().position((data.location)).title(data.name));
} else {
marker.setPosition(data.location);
cabsMarker.remove(data.name);
}
updatedMarkers.put(data.name, marker);
Toast.makeText(getApplicationContext(), "jkbkjbl", Toast.LENGTH_LONG).show();
}
// all markers that are left in markers list need to be deleted from the map
for (Marker marker : cabsMarker.values()) {
marker.remove();
}
cabsMarker = updatedMarkers;
}
任何人都可以幫助我是我錯了嗎?