简体   繁体   English

如何在Android中从Google Map v2清除圆圈和标记

[英]How to clear the circle and marker from the Google Map v2 in android

I am trying to draw a circle on the Google Map v2 in android with center as the current location as soon as the location gets changed. 我想在位置更改后立即在android的Google Map v2上绘制一个以center为当前位置的圆。 Now what I am seeing is, everytime when the location gets changed, circle keeps getting drawn(overlapping each other if the location is same) without deleting the previous circle. 现在,我所看到的是,每次更改位置时,圆都会不断绘制(如果位置相同,则彼此重叠)而不会删除前一个圆。 And same thing is happening with the Marker as well. 标记也发生了同样的事情。

Below is the code I am using to draw the circle on the Google Map v2 以下是我用来在Google Map v2上画圆的代码

@Override
public void onLocationChanged(Location location) {
    if (location != null) {

    // Create a LatLng object for the current location
    LatLng latLng = new LatLng(location.getLatitude(), location.getLongitude());

    // Show the current location in Google Map
    map.moveCamera(CameraUpdateFactory.newLatLng(latLng));

    // Zoom in the Google Map
    map.animateCamera(CameraUpdateFactory.zoomTo(14));

    CircleOptions circleOptions = new CircleOptions().center(latLng) // set center
    .radius(1000) // set radius in meters
    .fillColor(Color.TRANSPARENT) // default
    .strokeColor(0x10000000).strokeWidth(5);

    myCircle = map.addCircle(circleOptions);

    map.addMarker(new MarkerOptions().position(latLng).title("You are here!"));
}

How do I make sure, that whenever the circle is getting drawn next time, previous circle and marker is cleared from the Google Map. 如何确定下一次绘制圆时,会从Google地图中清除上一个圆和标记。 What changes I need to make in my code? 我需要在代码中进行哪些更改?

Any help will be appreciated. 任何帮助将不胜感激。

Removing things from the map is simple. 从地图中删除内容很简单。 For whatever reason, in GoogleMaps v2 you cannot simply remove a Marker by getting its id since that value is generated automatically on creation and thus pretty much useless. 无论出于何种原因,在GoogleMaps v2中,您都无法简单地通过获取其ID来删除Marker ,因为该值是在创建时自动生成的,因此几乎没有用处。 To work around this, all you need to do is create something that can store a reference to the object you want to remove. 要解决此问题,您要做的就是创建可以存储对要删除的对象的引用的内容。 One way to do this is to create HashMap and store a reference to your Marker , circle, or anything else you want the ability to remove, with some unique id. 一种实现方法是创建HashMap并使用一些唯一的ID存储对您的Marker ,圆或您想要删除的其他任何内容的引用。 By storing a reference to things you place on the map in a HashMap however you can then call remove on the marker associated with that key each time your location updates. 通过将对您放置在地图上的事物的引用存储在HashMap但是您可以在每次位置更新时调用与该键关联的标记上的remove。 The same goes for the circle (though you will need a different HashMap if you set the types as I have below - I don't know if you can use a generic String, Object map that will store both). 循环也是一样(尽管如果按照下面的方式设置类型,您将需要不同的HashMap我不知道您是否可以使用将两者存储的通用String,Object映射)。

To use this method, declare your HashMap like its an instance variable so it can be accessed from all the methods within your Activity 要使用此方法,请像实例变量一样声明HashMap以便可以从Activity所有方法中访问它

private HashMap<String, Marker> mapStuff = new HashMap<String, Marker>();

Then wherever you create your Marker or other map objects, just add them to the HashMap with some key value 然后,无论您在哪里创建Marker或其他地图对象,只需将它们添加到具有某些键值的HashMap

Marker dude = map.addMarker(new MarkerOptions()
.position(newLatLng(latitude, longitude))
.title(MARKER_TITLE)
.snippet(SNIPPET));
mapStuff.put("userMarker", dude);

The concept is really the same as what was suggested by another poster, which is just to have a Marker as an instance variable and store a reference there. 这个概念实际上与另一个发布者所建议的相同,后者只是将Marker作为实例变量并在其中存储引用。 Both do the same thing. 两者都做同样的事情。 The HashMap approach works best if you're dealing with multiple markers or objects. 如果您要处理多个标记或对象,则HashMap方法最有效。 If you're only dealing with one Marker or one circle, the single variable approach is probably more correct since you do not need to use a Collection to add or remove one marker. 如果只处理一个Marker或一个圆,则单变量方法可能更正确,因为您不需要使用“ Collection来添加或删除一个标记。 Just declare 只需声明

private Marker userMarker;

and then where you add the Marker to the map, store a reference 然后在将Marker添加到地图的位置存储参考

userMarker = map.addMarker(new MarkerOptions()
.position(newLatLng(latitude, longitude))
.title(MARKER_TITLE)
.snippet(SNIPPET));

When you update your location, probably in onLocationChanged just check for the existance of the marker and remove it if present and re-add 更新位置时,可能在onLocationChanged只需检查标记的存在并将其删除(如果存在)并重新添加

if(userMarker == null){
    displayUserMarker(location);
} else {
    userMarker.remove;
    displayUserMarker(location);
}
map.clear();

add this before if (location != null) 在if(location!= null)之前添加

So that every time the location changes all the markers and circle is removed and drawn again 这样,每次位置更改时,所有标记和圆圈都会被删除并再次绘制

First of all, keep a reference to marker just like you keep to circle 首先,像继续圈出一样,保持对标记的引用

myMarker = map.addMarker(new MarkerOptions().position(latLng).title("You are here!"));

When you want to remove them, just call remove() : 当您想删除它们时,只需调用remove()

myCircle.remove();
myMarker.remove();

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

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