2014-12-04 99 views
2

我是android編碼的新手,並試圖抓住它。我正在學習教程,並在線閱讀材料和書籍。我開始有點自信,因此轉移到更復雜的任務上。如何在Android上的Google地圖上隱藏和顯示標記?

我與谷歌地圖的工作,並已成功地顯示在推出的地圖上我的指定位置。

//Making array businessNY global 

public static ArrayList<LatLng> businessesNY; 


//Creating a method showBNY to add given coordinates to the array and then add those arrayed coordinates on the map with given perimeter. I call this method on my onCreate. 

public static boolean showBNY(){ 
     businessesNY = new ArrayList<LatLng>(); 
     businessesNY.add(new LatLng(40.741532, -73.989293)); 
     businessesNY.add(new LatLng(40.684281, -73.996159)); 

     for(LatLng bNY : businessesNY){ 
      mMap.addMarker(new MarkerOptions() 
      .position(bNY) 
      .title("New York Business") 
     } 

     return true; 
    } 

我想下一步要做的是創建2或1可能的方法來隱藏這些地點或顯示出他們,如果隱藏,反之亦然。我正在讀這篇文章,並確定你必須使用.setVisible(false)隱藏和.setVisible(true);來顯示。我想讓用戶點擊一個按鈕來切換,同一個按鈕切換。

我想不通我怎麼能實現這一點,我一直在努力要長期現在是越來越令人沮喪,我會很感激的任何幫助。

可能有人請幫助我。

感謝

回答

0

試試這個

Marker markers = gMap.addMarker(new MarkerOptions() 
       .position(latlng) 
       .title("MyPlace").icon(BitmapDescriptorFactory.fromResource(R.drawable.ic_pin)).draggable(true)); 

在您的按鈕的Click事件

切換按鈕

<ToggleButton 
    android:id="@+id/togglebutton" 
    android:layout_width="wrap_content" 
    android:layout_height="wrap_content" 
    android:textOn="On" 
    android:textOff="Off" 
    android:onClick="onToggleClicked"/> 

那麼只有處理方法需要在被定義活動Java

public void onToggleClicked(View view) { 
    if(((ToggleButton) view).isChecked()) { 
      markers.setVisible(true); 
     // handle toggle on 
    } else { 
     markers.setVisible(false); 
     // handle toggle off 
    }  
} 

+0

我想隱藏的陣列 – David 2014-12-04 07:48:33

+0

中的所有標記是否可以讓你把我的值和變量在代碼方面,它會更簡單,我聽不懂。我看到你正在給地圖添加標記,然後給我一個代碼來刪除這些標記。我不明白我如何將它應用於我的代碼。 = – David 2014-12-04 07:54:21

+0

我的標記已經在地圖上顯示在我想要發佈的地圖上,接下來我要做的是讓用戶選擇隱藏/取消隱藏它們。 – David 2014-12-04 07:55:33

相關問題