1

我想讓用戶從地圖中刪除一個點(或更具體地說,一個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(); 
} 

當我點擊一個現有的點後,會出現一個對話框提供刪除點。當我選擇刪除點時,點仍然保留在屏幕上。如果我放置一個新的點,那麼刪除的點會被刪除。但是,如果我單擊「已刪除」點或其他現有點,程序就會因此錯誤而崩潰。

error

如果你有做什麼線索,我會很感激您的來信!

編輯

在從Vishwa帕特爾尖端,我從地圖馬上使用postInvalidate()除去的點。然而,當我點擊圖標所在的位置時,我仍然會收到indexoutofbounds例外情況。

+0

代替'com.example.mapproject.MainActivity.mapView.invalidate();'嘗試'v.invalidate()''中的onClick(視圖v)....' – tozka

+1

你有打電話給'填充( )'再次?基本上ItemizedOverlay是要做很多事先預備。如果更新數據集,則必須有一種方法來重新創建所有緩存。首次創建此結構時調用填充。如果不允許在同一個對象上多次調用,則可以執行一些操作。爲drawable創建一個新的狀態並僅僅關閉它(不可見,忽略命中),或者你可以重新創建整個ItemizedOverlay並將其添加到MapView覆蓋集合中 –

回答

1

我相信我找到了解決方案here。它似乎工作至今,答案是把下面一行到我removeOverlay方法,

setLastFocusedIndex(-1); 

的代碼從我的自定義覆蓋刪除OverylayItem是,

protected void removeOverlay(OverlayItem overlayItem) { 

    mapOverlays.remove(overlayItem); 
    MainActivity.mapOverlays.remove(this); 
    setLastFocusedIndex(-1); 
    populate(); 

} 

任何想法/建議受歡迎的!

2

您可能需要致電上的invalidate(),迫使其重新繪製自己。正如評論者所提到的,您可能還需要重新撥打populate()。您的應用可能會崩潰,因爲它正嘗試致電onTap(),查看不存在的OverlayItem。您可能還想嘗試任何可以「刷新」MapView和/或Overlay的方法,因爲這是您需要做的以使OverlayItem消失。

+0

我試過這些方法,它們幫助刪除圖標,但是如果我點擊那裏的圖標已經是,我會得到一個數組越界的錯誤,我發現一個頁面記錄類似的問題,似乎修復它! http://developmentality.wordpress.com/2009/10/19/android-itemizedoverlay-arrayindexoutofboundsexception-nullpointerexception-workarounds/ – TomSelleck

+0

我很高興你能找到解決方案!祝好運與項目的其餘部分。 – crocboy

1

使用

com.example.mapproject.MainActivity.mapView.postInvalidate(); 

,因爲你正在從非UI線程的無效電話,爲postInvalidate的文檔中指定()嘗試;

+0

嘿,這對於擺脫圖標馬上起作用!但是,仍然有幾個問題..如果你點擊圖標的位置,應用程序將崩潰與索引outofbounds例外.. – TomSelleck

+1

@Tomcelic解決這個問題我想你還需要從列表中刪除覆蓋存在於MapActivity類中的mapOverlays(我假設您遵循官方開發者教程,該類應稱爲HelloGoogleMaps)。因此,您必須將該列表設爲靜態,並從該列表中刪除此特定覆蓋圖(您可以通過簡單地說HelloGoogleMaps.mapOverlays.remove(this)來完成此操作;這應該可以做到這一點)。 –

+0

不,似乎沒有做到這一點:( – TomSelleck