我想讓用戶從地圖中刪除一個點(或更具體地說,一個OverlayItem)。我跟着開發人員tutorial開始實施CustomMapView,在this教程中捕獲地圖上的長按。從地圖中刪除OverylayItem
所以現在我有一個程序允許用戶在地圖上放置點。我的下一個目標是讓用戶刪除點。這是我的代碼,當用戶點擊地圖上的現有點時。
public class OurItemizedOverlay extends ItemizedOverlay {
//Create new list of points
private ArrayList<OverlayItem> mapOverlays = new ArrayList<OverlayItem>();
private Context mapContext;
@Override
protected boolean onTap(final int index) {
Button edit, remove;
//Get index of item tapped
OverlayItem item = mapOverlays.get(index);
//Create Dialog to show point info, allow for edit or removal.
LinearLayout layout = new LinearLayout(mapContext);
layout.setOrientation(LinearLayout.VERTICAL);
LayoutInflater inflater = LayoutInflater.from(mapContext);
AlertDialog.Builder builder = new AlertDialog.Builder(mapContext);
builder.setTitle(item.getTitle());
builder.setMessage(item.getSnippet());
View view = inflater.inflate(R.layout.view_or_edit_location_dialog, null);
builder.setView(view);
builder.show();
//BUTTONS
edit = (Button)view.findViewById(R.id.edit);
remove = (Button)view.findViewById(R.id.delete);
//Edit Button Listener
edit.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
}
});
//Remove Button Listener
remove.setOnClickListener(new View.OnClickListener() {
@Override
public void onClick(View v) {
removeOverlay(index); <--------
Log.d("View location info", "user clicked delete.");
return;
}
});
return true;
}
這是我的removeOverlay代碼。
protected void removeOverlay(int index) {
mapOverlays.remove(index);
com.example.mapproject.MainActivity.mapView.invalidate();
}
當我點擊一個現有的點後,會出現一個對話框提供刪除點。當我選擇刪除點時,點仍然保留在屏幕上。如果我放置一個新的點,那麼刪除的點會被刪除。但是,如果我單擊「已刪除」點或其他現有點,程序就會因此錯誤而崩潰。
如果你有做什麼線索,我會很感激您的來信!
編輯
在從Vishwa帕特爾尖端,我從地圖馬上使用postInvalidate()除去的點。然而,當我點擊圖標所在的位置時,我仍然會收到indexoutofbounds例外情況。
代替'com.example.mapproject.MainActivity.mapView.invalidate();'嘗試'v.invalidate()''中的onClick(視圖v)....' – tozka
你有打電話給'填充( )'再次?基本上ItemizedOverlay是要做很多事先預備。如果更新數據集,則必須有一種方法來重新創建所有緩存。首次創建此結構時調用填充。如果不允許在同一個對象上多次調用,則可以執行一些操作。爲drawable創建一個新的狀態並僅僅關閉它(不可見,忽略命中),或者你可以重新創建整個ItemizedOverlay並將其添加到MapView覆蓋集合中 –