简体   繁体   English

Google Maps api v2 android:如何删除单个标记?

[英]Google Maps api v2 android: How to Remove a single marker?

In my app i need to have 2 types of markers: the first ones need to stay on a location and the second ones need to move, right now I dont have the static markers yet but my app can show a marker moving if the location of the phone changes but for that i call mMap.clear(), I dont want to clear all markers when the location changes so I need to remove only that marker, I read in another question that I need to use Marker.remove(); 在我的应用中,我需要有两种类型的标记:第一种需要停留在一个位置上,第二种需要移动,现在我还没有静态标记,但是如果手机变了,但是为此我打电话给mMap.clear(),我不想在位置改变时清除所有标记,所以我只需要删除那个标记,我读了另一个问题,我需要使用Marker.remove(); to remove individual markers but im not sure where to implement that in the code. 删除单个标记,但不确定在代码中的哪里实现。

Here is the method for a new location: 这是新位置的方法:

public void onLocationChanged(Location location) {
    mMap.clear();
    GetLatLong();
    handleNewLocation(location);
    mCurrentLocation = location;


}

and here is the handleNewLocation method: 这是handleNewLocation方法:

    private void handleNewLocation(Location location) {
    if (mLastLocation != null) {

        LatLng latLng = new LatLng(list.get(0), list.get(1));
        //mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


        //añadir un nuevo marker con la posición de la variable latLng
        MarkerOptions camion = new MarkerOptions()
                .position(latLng)
                .title("Camión")
                .snippet("ruta " + ruta)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus));


        Marker marker = mMap.addMarker(camion);
        if (marker == null) {
            mMap.addMarker(camion);
        } else {
            camion.position(latLng);

        }

    }
}

Thank you. 谢谢。

EDIT: 编辑:

List<Marker> markers = new ArrayList<Marker>();
@Override
public void onLocationChanged(Location location) {
    markers.clear();
    GetLatLong();
    handleNewLocation(location);
    mCurrentLocation = location;
}
private void handleNewLocation(Location location) {
    if (mLastLocation != null) {

        LatLng latLng = new LatLng(list.get(0), list.get(1));
        //mMap.moveCamera(CameraUpdateFactory.newLatLng(latLng));


        //añadir un nuevo marker con la posición de la variable latLng
        MarkerOptions camion = new MarkerOptions()
                .position(latLng)
                .title("Camión")
                .snippet("ruta " + ruta)
                .icon(BitmapDescriptorFactory.fromResource(R.drawable.bus));


        Marker marker = mMap.addMarker(camion);

        if (marker == null) {

            markers.add(marker);
        } else {
            camion.position(latLng);

        }

    }
}

} }

If you want to remove one marker - you have to remember this marker first. 如果要删除一个标记,则必须先记住该标记。 Then call marker.remove() . 然后调用marker.remove()

Code: 码:

class YourClass {
    Marker singleMarker; //marker to be removed

    public void addMarker() {
        ....
        //here you add your marker
        singleMarker = mMap.addMarker(camion);
    }

    public void removeSingleMarker() {
        if(singleMarker != null) {
            singleMarker.remove();
            singleMarker = null;
        }
    }
}

As I discussed above, you can try to save the what-you-want-remove marker in an ArrayList or in a HashMap . 如上文所述,您可以尝试将what-you-want-remove markerArrayListHashMap

Sample code: 样例代码:

// before loop:
List<Marker> markers = new ArrayList<Marker>();

// inside your loop:
Marker marker = myMap.addMarker(new MarkerOptions().position(new LatLng(geo1Dub,geo2Dub))); //...
markers.add(marker);

// after loop:
markers.size();

声明:本站的技术帖子网页,遵循CC BY-SA 4.0协议,如果您需要转载,请注明本站网址或者原文地址。任何问题请咨询:yoyou2525@163.com.

 
粤ICP备18138465号  © 2020-2024 STACKOOM.COM