簡體   English   中英

Android> Google Maps>疊加層:點按不同的內容以使不同的事情發生

[英]Android > Google Maps > Overlay: Tap different things to make different things happen

我正在嘗試制作一個應用程序,其中當用戶點擊地圖的空白部分時,會出現一個新的標志,然后當他們點擊該標志時,會出現一個對話框。

我自己編寫了第一個onTap方法,然后從Google Maps教程中復制了第二個方法來開始使用。 問題是,第一個總是觸發,而第二個永遠不觸發。 如果刪除第一種方法,則第二種方法應按預期的方式工作(點按一個標志會使相應的對話框出現)。 這兩個都是ItemizedOverlay類中的方法,mContext是構造函數生成的上下文,而location是OverlayItems的ArrayList。

我的問題是,我該如何調和兩者?

    public boolean onTap(GeoPoint p, MapView mapView){
        locations.add(new OverlayItem(p, "Point 3", "Point 3"));
        populate();
        return false;
    }

    @Override
    protected boolean onTap(int index) {
      OverlayItem item = locations.get(index);
      AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
      dialog.setTitle(item.getTitle());
      dialog.setMessage(item.getSnippet());
      dialog.show();
      return true;
    }

問題在於,通過實現/覆蓋onTap(GeoPoint p, MapView mapView)您將無法運行ItemizedOverlay對該方法的實現,而該方法本身通常會調用onTap(int index)

您想要更多類似...

public boolean onTap(GeoPoint p, MapView mapView){
    if (super.onTap(p, mapView)) 
        return true; 

    locations.add(new OverlayItem(p, "Point 3", "Point 3"));
    populate();
    return false;
}

@Override
protected boolean onTap(int index) {
  OverlayItem item = locations.get(index);
  AlertDialog.Builder dialog = new AlertDialog.Builder(mContext);
  dialog.setTitle(item.getTitle());
  dialog.setMessage(item.getSnippet());
  dialog.show();
  return true;
}

希望能有所幫助。

暫無
暫無

聲明:本站的技術帖子網頁,遵循CC BY-SA 4.0協議,如果您需要轉載,請注明本站網址或者原文地址。任何問題請咨詢:yoyou2525@163.com.

 
粵ICP備18138465號  © 2020-2024 STACKOOM.COM