如何在Android中隐藏和显示谷歌地图上的标记?

如何在Android中隐藏和显示谷歌地图上的标记?,android,google-maps,map,tags,location,Android,Google Maps,Map,Tags,Location,我是一个android编码的新手,并试图掌握它。我正在学习教程,并在网上阅读材料和书籍。我开始有点自信了,因此我开始着手更复杂的任务 我正在使用谷歌地图,并在发布时设法在地图上显示我指定的位置 //Making array businessNY global public static ArrayList<LatLng> businessesNY; //Creating a method showBNY to add given coordinates to the array

我是一个android编码的新手,并试图掌握它。我正在学习教程,并在网上阅读材料和书籍。我开始有点自信了,因此我开始着手更复杂的任务

我正在使用谷歌地图,并在发布时设法在地图上显示我指定的位置

//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;
    }
//使阵列业务全球化
公共静态阵列列表业务纽约;
//创建一个showBNY方法,将给定的坐标添加到数组中,然后在地图上以给定的周长添加这些阵列坐标。我在onCreate上调用此方法。
公共静态布尔showBNY(){
businessesNY=newarraylist();
businesssny.add(新LatLng(40.741532,-73.989293));
businesssny.add(新LatLng(40.684281,-73.996159));
(纽约州:商业纽约市){
mMap.addMarker(新标记选项()
.职位(纽约)
.名称(“纽约商业”)
}
返回true;
}
下一步我要做的是创建2个或可能1个方法来隐藏这些位置,或者在隐藏时显示它们,反之亦然。我正在阅读相关内容,并确定您必须使用
.setVisible(false)
来隐藏和
.setVisible(true)
显示。我希望允许用户单击一个按钮来打开,同时单击同一个按钮来关闭

我不知道如何实现这一点,我已经在这方面工作了很长时间,它正在变得令人沮丧,我真的非常感谢任何帮助

谁能帮我一下吗

谢谢你试试这个

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

切换按钮

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


您是否可以将我的值和变量放在您的代码上下文中,这将使我更容易理解。我看到您正在向地图添加标记,然后给我一个代码来删除这些标记集。我不知道如何将其应用于我的代码。=我的标记在启动时已显示在地图上,我想要,什么“我下一步要做的是给用户一个选项,让他们在will上隐藏/取消隐藏它们。@David和我建议的一样,迭代该列表并在所有这些列表上设置Visible(false)。我可以问一下Marker和LatLngI之间的区别吗?Gi正在阅读一些材料,他们似乎也使用Marker而不是LatLng。
public void onToggleClicked(View view) {
    if(((ToggleButton) view).isChecked()) {
          markers.setVisible(true); 
        // handle toggle on
    } else {
        markers.setVisible(false); 
       // handle toggle off
    }    
}